Combinatix
Combinatix

Reputation: 1216

Array constructor with function pointers?

I'm trying to create a task list which should be used in task scheduler later in main loop(). I tried to use constructor but compiler throws an error 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() ...

What would be the simplest way of achieving this? I can do a function to fill the task array manually but it looks ugly and it is not very readable. I have seen some similar questions but they were about classes and too complicated for me :/

Upvotes: 0

Views: 83

Answers (2)

Combinatix
Combinatix

Reputation: 1216

Oh, I got it. The error is in the struct:

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

After removing it, it compiles fine.

Upvotes: 1

lakeweb
lakeweb

Reputation: 1939

See if your compiler is looking for a constructor:

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

Upvotes: 0

Related Questions