简体   繁体   中英

Arduino method name clashes with library function

I have this simple blink example, modified to declare a class whose only method signature matches that of the delay library function. It crashes the Arduino unless I rename the method. I see that the Arduino.h header has the "extern C" linkage specifier, so there should not be any name conflicts. Could you help me understand this error?

Regards.

class Wrapper
{
public:
  void delay(unsigned long t)
  {
    delay (t); 
  }
};

Wrapper wr;

Wrapper* wrp = ≀


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  wrp->delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  wrp->delay(1000);              // wait for a second
}

The code as listed has a stack overflow issue. Within Wrapper::delay(unsigned long) , delay(t) calls Wrapper::delay again rather than the Arduino delay() routine .

If you want to call the Arduino delay() routine within Wrapper::delay , you need to qualify the call like so:

class Wrapper
{
public:
  void delay(unsigned long t)
  {
    ::delay(t);
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM