Atmega328P/ESP Arduino Operator Overload Problem

Recently I've gotten into operator overloading mostly out of curiosity so I don't know much about it yet.

For some reason, I could only get it to work(compile) on the Atmega 328P and not the ESP32 or the ESP8266(Node MCU). The sketch below will only compile for the Atmega328P(with warnings) but not on either of the ESP's.

I don't know what is going on, it is probably because all three compilers work differently even though they are technically C++11(I think). Maybe I am using this stuff incorrectly. Maybe someone who knows this stuff might know. Any help will be greatly appreciated!

Board manager:

  • Arduino AVR Boards Ver 1.8.5 by: Arduino
  • esp32 Ver 1.0.6 by: Espressif Systems
  • esp8266 Ver 3.0.2 by: ESP8266 community

Software:

  • Arduino IDE Ver 1.8.19 w/ VsCode

|

Operator Overloading Sketch:

class foo
{
  public:
    foo(int16_t Val)
    {
      _Val = Val;
    }
    foo &operator=(int16_t Val)
    {
      _Val = Val;
    }
    operator int16_t()
    {
      return _Val;
    }

  private:
    int16_t _Val = 0;
};

void setup() {
  Serial.begin(115200);
  Serial.println("\nInit");
  foo obj = 6;
  Serial.print("obj = ");
  Serial.println(obj);
  obj = 12;
  Serial.print("obj now = ");
  Serial.println(obj);
}

void loop() {

}

|

|

Warnings/Errors:

The Sketch gives these warnings for Atmega328p:

C:\Users\AVG\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\cores\arduino\new.cpp:59:60: warning: unused parameter 'tag' [-Wunused-parameter]
void * operator new(std::size_t size, const std::nothrow_t tag) noexcept {
^~~
C:\Users\AVG\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\cores\arduino\new.cpp: In function 'void* operator new [](std::size_t, const std::nothrow_t&)':
C:\Users\AVG\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\cores\arduino\new.cpp:68:63: warning: unused parameter 'tag' [-Wunused-parameter]
void * operator new[](std::size_t size, const std::nothrow_t& tag) noexcept {
^~~
C:\Users\AVG\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\cores\arduino\new.cpp: In function 'void operator delete(void*, const std::nothrow_t&)':
C:\Users\AVG\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\cores\arduino\new.cpp:103:55: warning: unused parameter 'tag' [-Wunused-parameter]
void operator delete(void* ptr, const std::nothrow_t& tag) noexcept {
^~~
C:\Users\AVG\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\cores\arduino\new.cpp: In function 'void operator delete [](void*, const std::nothrow_t&)':
C:\Users\AVG\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\cores\arduino\new.cpp:106:57: warning: unused parameter 'tag' [-Wunused-parameter]
void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept {
                                                         ^~~

It gives out this error for the 32(Same Sketch):

OperatorOverLoadProblemESP:11:5: error: no return statement in function returning non-void [-Werror=return-type]
}
     ^

and this for the 8266(Same Sketch):

OperatorOverLoadProblemESP:11:5: error: no return statement in function returning non-void [-Werror=return-type]
10 |       _Val = Val;
  +++ |+      return *this;
   11 |     }
      |     ^

I don't know why i didn't try this earlier but if you add: return *this; inside : foo &operator=(int16_t Val), the problem completely goes away for the ESP's but I would still like to know why I have to do this and why only for the ESP's, not Atmega328p. Also, why is the Atmega328p(AVR) compiler still spitting out these warnings? Should I be worried about them?

The compiler is just pointing out some questionable code in the "new.cpp" file in the Arduino core. The 'new' and 'delete' functions declare an argument that is never used. The core authors should cast those arguments to void somewhere to get rid of the warnings.

They are not your code so you just have to ignore the warnings. The next time you compile your sketch it will use the cached core and you won't see those warnings again.

Oh, your right. For some reason, I just assumed it was one of the cached files the compiler used to copy the sketch too because of the generic name(new.cpp) and that is was somehow connected to my sketch.

The warnings persist even when I remove all trace of Operator overloading. I just never noticed them before until now

Thank you

One other questions, How deos "*this" work? For some reason I cant search it up, Maybe I dont have the right name, I keep getting "-> this" in the results instead.
"return *this;" is I what I had to add to get the sketch to work for the esp's

do a web serach for *C++ this, e.g. C++ this pointer

Ok, yes but what I don't understand is what the asterisk "*" does in this scenario. I thought the "this" keyword was a pointer itself, why does it need the asterisk? I have always seen people use the "this" keyword with the arrow"->" operator only to point a specific variable or method to a structure or class, never with the asterisk "*".

have a look at what-does-return-this-mean-in-c

  • is the dereferencing operator

Got it, makes more sense now. thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.