包含函数指针的数组构造函数?

时间:2016-12-30 23:30:06

标签: 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() ...

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

2 个答案:

答案 0 :(得分:1)

哦,我明白了。错误在结构中:

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

删除后,它编译得很好。

答案 1 :(得分:0)

查看您的编译器是否正在寻找构造函数:

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