0

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 :/

5
  • 2
    Do you have a forward declaration for the functions? Commented Dec 30, 2016 at 23:32
  • Also, it's purely a stylistic preference but when I want to initialize a function pointer, I use the address-of operator &functionname just like I would do if making a data pointer. The bare function name will decay to a pointer, but it's not as clear to a reader that you are dealing with a function pointer as opposed to a function object of some type. Commented Dec 30, 2016 at 23:33
  • On a C++ compiler, assuming correct prototypes, it compiles fine for me: rextester.com/RXVP25357 Perhaps your compiler doesn't support the C++ standard? Commented Dec 30, 2016 at 23:36
  • Guys, I'm new to C, I don't really know what is the difference between C and C++. This is Arduino 1.6.8 compiler. Commented Dec 30, 2016 at 23:43
  • @BenVoigt Probably not. I do not know what it is.. sorry :) Commented Dec 30, 2016 at 23:47

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ahh yes. And why the constructor mitigates the problem. One up.
Aggregate property changes in this regards from C++11 to C++14. In C++11, that initialization in class avoid the class to be an aggregate. It is no longer true in C++14.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.