c++ - 错误 : 'current_millis' was not declared in this scope

标签 c++ scope arduino header destructor

你的意思是我的公共(public)类变量没有在范围内声明,我不只是在头文件中声明它>:(

IDE arduino 1.6.12 arduino type uno 这应该无关紧要只需要它来验证

timelyCall.cpp

#include "Arduino.h"
#include "timelyCall.h"
timelyCall::timelyCall() {
  current_millis =  millis();
}

timelyCall::~timelyCall() {

这里有错误/

  delete current_millis;
}

void timelyCall::callEvery(void (&f)(), int ms) {
  if (millis() - current_millis > ms) {
    f();
    current_millis = millis();
  }
}

void setCurrentMillis() {
  current_millis = millis();
}

timelyCall.h

class timelyCall {
  public:
    timelyCall();
    ~timelyCall();
    unsigned long current_millis;
    void callEvery(void (&f)(), int ms);
  };

完全错误

Arduino: 1.6.12 (Windows 7), Board: "Arduino/Genuino Uno"

sketch\timelyCall.cpp: In destructor 'timelyCall::~timelyCall()':

timelyCall.cpp:8: error: type 'long unsigned int' argument given to 'delete', expected pointer

   delete current_millis;

          ^

sketch\timelyCall.cpp: In function 'void setCurrentMillis()':

timelyCall.cpp:19: error: 'current_millis' was not declared in this scope

   current_millis = millis();

   ^

exit status 1
type 'long unsigned int' argument given to 'delete', expected pointer

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

最佳答案

不要在析构函数中删除 current_mills。您应该只删除动态分配的变量。换句话说,如果您使用 new 创建对象,则以 1:1 的比例使用 delete 删除它。

至于第二个错误,timelyCall 类中没有定义setCurrentMills() 函数。

你必须修改类定义:

class timelyCall {
  public:
    timelyCall();
    ~timelyCall();
    unsigned long current_millis;
    void setCurrentMills();
    void callEvery(void (&f)(), int ms);
  };

然后将函数定义更改为 void timelyCall::setCurrentMills()

此外,将要分配的值传递给函数 set 也是一种标准。因此,将此签名更改为 void setCurrentMills(const unsigned long& mills); 而不是将函数 mills() 的返回值分配给current_mills,使用此函数的返回值调用 setter,如下所示 object.setCurrentMills(mills());

关于c++ - 错误 : 'current_millis' was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40935080/

相关文章:

java - 当变量超出范围时,它永远不能回到范围内

c# - 从 ASP.NET 中的标记访问我的代码隐藏类属性的最佳方法

c++ - 在类中调用另一个构造函数作为全局

windows - Cygwin - 串行端口列在/dev 但 stty 报告无效参数

c++ - 如何在 C++ 中被 EOF 停止后恢复输入流?

android - Cocos2d-x crash due to ccLayer->schedule(schedule_selector(...), ...);

c++ - 如何将未知大小的二维数组传递给函数

c++ - C++库应如何允许自定义分配器?

python - 关闭 : retrieve a variable given with the outer function

shell - arduino yun与USB声卡获取文件数