Passing member function pointer to EventQueue call_every and call_in?

#include "mbed.h"
class EventHandler {
    int _id;
public:
    EventHandler(int id) : _id(id) { }
    void handler(int c, void *f) {
        printf("ID: %d Param: %d\r\n", _id, c);
    }
};

void foo() {
}

int main() {
    // creates a queue with the default size
    EventQueue queue;
    // Create EventHandler object with state
    EventHandler handler_cb(3);
    // events are simple callbacks, call object method in 2 seconds
    // with provided parameter
    queue.call_in(2000, &handler_cb, &EventHandler::handler, 4, foo);
    // the dispatch method executes events
    queue.dispatch();
}

I took the example from the documentation, and modified it to add a simple function pointer to handler(). And I am getting the compiler error

Error: Expression preceding parentheses of apparent call must have (pointer-to-) function type in “extras/mbed-os.lib/events/EventQueue.h”, Line: 2342, Col: 14

Does anyone know how can a function pointer be passed?

Hi Andre,

I think you should write like this

void handler(int c, void (*f)()) {
    printf("ID: %d Param: %d\r\n", _id, c);
}

Regards,
Desmond

1 Like