简体   繁体   English

Arduino方法名称与库函数冲突

[英]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. 除非我重命名该方法,否则它会使Arduino崩溃。 I see that the Arduino.h header has the "extern C" linkage specifier, so there should not be any name conflicts. 我看到Arduino.h标头具有“ extern C”链接说明符,因此不应有任何名称冲突。 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 . Wrapper::delay(unsigned long)delay(t)再次调用Wrapper::delay而不是Arduino delay()例程

If you want to call the Arduino delay() routine within Wrapper::delay , you need to qualify the call like so: 如果要在Wrapper::delay调用Arduino delay()例程,则需要对调用进行限定,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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