c++ - 带有函数指针的数组构造函数?

标签 c++ arduino

我正在尝试创建一个任务列表,该列表应该稍后在主循环()中的任务调度程序中使用。我尝试使用构造函数,但编译器抛出错误 could not covert '{doKeypad,2000,0}' from '<brace-enclosed initializer list>' to 'Task'

struct Task{
    void (*proc)();                     // Process callback
    unsigned long dly;                  // delay in ms
    unsigned long mls = 0;              // last run in millis()
};

Task task[] = {                         // This is much more readable 
    {doKeypad, 2000, 0},                // but it does not work :)
    {doPower,    10, 0},
    {doDallas,  800, 0},
    {doLcd,     500, 0}
};

void doKeypad(){
     // some code here... 
}
// rest of code follows - doPower(), doDallas() ...

实现此目标的最简单方法是什么?我可以做一个函数来手动填充任务数组,但它看起来很难看,而且可读性不是很好。我见过一些类似的问题,但它们是关于类(class)的,对我来说太复杂了:/

最佳答案

哦,我知道了。错误在结构中:

struct Task{
    void (*proc)();
    unsigned long dly;
    unsigned long mls = 0;  // < There should not be = 0
};

删除它后,它可以正常编译。

关于c++ - 带有函数指针的数组构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41403974/

相关文章:

c++ - Arduino代码: shifting bits seems to change data type from int to long

c++ - 类中的重载方法(arduino)

c++ - FFmpeg/libav 将 AVPacket 数据存储在文件中并在不同的流中再次解码它们 - 如何在解码器中准备和发送自定义数据包?

c++ - 如何在 C++ 中将 LPBYTE 转换为 char */string?

c++ - OpenGL 移动对象不起作用

java - 以编程方式扫描蓝牙设备在 Android 6+ 中找不到任何内容

arduino - 如何在类中实例化 ESP AsyncWebServer? (arduino esp8266)

c++ - 实时使用opencv的光流

python - 如何在 OpenCV 中找到二进制骨架图像的端点?

c++ - 无法在 Ubuntu、C++ 上配置设备 ttyUSB0 (Arduino)