Search code examples
c++functionpointersvoid

how to pass a void function to a void pointer in C++


Here is my problem: I have my tact class which is used for tactile buttons in embedded applications.

I added the setFunctions() function so that I can link 3 functions to each tact instance I create (which would deal with short buttons presses, releases and long presses).

class tact
{
public:
tact(int assigned_pin, input_shift_register shift = {0,0,0}); // Constructor

void debounce();
short poll(bool debounce_flag);
void activate();
void setFunctions(void short_press_function(void), void release_press_function(void), void long_press_function(void));

short state;

private:
    int pin;
    void (*short_ptr)(void);
    void (*release_ptr)(void);
    void (*long_ptr)(void);
}

Therefore I could create 3 unique functions in main for each tact instance I need and link them to the tact instance with the setFunctions() member.

This is how the setFunctions member works (or is supposed to). It links the 3 unique functions to 3 void pointers which are part of the tact class under private:

void tact::setFunctions(void short_press_function(), void release_press_function(), void long_press_function())
{
    short_ptr = short_press_function;
    release_ptr = release_press_function;
    long_ptr = long_press_function;
}

Then, the pointers are used to access the functions from the activate member (a simple switch case to see in which state the tact button currently is):

void tact::activate()
{
    switch (tact::state)
    {
    case SHORT_EFFECT_REQUIRED:
        short_ptr();
        break;

    case RELEASE_EFFECT_REQUIRED:
        release_ptr();
        break;

    case LONG_EFFECT_REQUIRED:
        long_ptr();
        break;

    default:
        break;
    }
    tact::state = 0;
}

In main, I plan on doing this:

void up_short()
{
}
void up_release()
{
}
void up_long()
{ 
}

tact upPin(2);

void setup()
{
  upPin.setFunctions(up_short(), up_release(), up_long()); //ERRORS ARE HERE!!
}

 void loop()
 {
  //use the tact buttons in my applications!
  upPin.poll(DEBOUNCED); // Updates upPin.state

  if(upPin.state)  //if not 0, either pressed, released, or pressed long enough
    upPin.activate();
 }

but the setFunctions() mentions 3 times the same error:

argument of type "void" is incompatible with parameter of type "void (*)()"

one error for each of the 3 functions I am passing as arguments to setFunctions()

So my question is what am I doing wrong? I really liked how I could simply use upPin.activate to reach my functions and the right one is done automatically.. I do no understand why setFunctions() won't let me link void functions to my void function pointers???

Thank you!


Solution

  • The line:

    upPin.setFunctions(up_short(), up_release(), up_long())
    

    is invoking the functions up_short(), up_release(), and up_long() each individually. These functions return void, which is then passed to each parameter in setFunctions -- which is why you are experiencing this error 3 times.

    What you want to do is pass (pointers to) the functions themselves to setFunctions

    If you change the line to be:

    upPin.setFunctions(up_short, up_release, up_long);
    

    You should see this work