error: 'current_millis' was not declared in this scope

error: 'current_millis' was not declared in this scope

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

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);
  };

FullError

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 创建对象,则使用 delete 以 1:1 比率删除它。

至于第二个错误,timelyCallclass里面没有定义setCurrentMills()函数。

您必须修改 class 定义:

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() 的 return 值分配给 current_mills,而是调用 setter,这对于将来可能的修改将非常有益return 这个函数的值像这样 object.setCurrentMills(mills());

不要在析构函数中删除 current_millis,只删除使用 new 创建的内容。