0

I am new to event handling in C++ and got stuck at point where I am trying to call event handler functions from the object of the structure.

typedef struct TaskScheduler
{
    uint32_t taskId;
    uint32_t taskTimeoutInMs;
    TaskState_t taskState;
    TaskType_t taskType;
    void (EventHandlers::*callback)(void);
    uint32_t tasktime;
}Task_t;

class Handler
{
    public:
        void registerTask(uint32_t taskId, void  (EventHandlers::*callback)(void));
};

void EXTI2_Handler()
{
     taskScheduler.*callback();
}

ERROR:must use '.' or '->' to call pointer-to-member function in 'task[((int)i)].Task::callback (...)', e.g. '(... ->* task[((int)i)].Task::callback) (...)' (task[i].callback());*

I am new to C++ and not able to resolve this error. i had seen other examples but now able to understand the resolution

Any help!

Vaibhav
  • 198
  • 1
  • 7
  • This doesn't help. – Vaibhav Feb 04 '21 at 17:16
  • It's very cumbersome to do function pointers like this. Using a wrapper like std::function will make your life way easier. – Leonardo G Feb 04 '21 at 18:47
  • @LeonardoG -- `std::function` isn't free; it adds significant overhead. If all you're doing is calling through a pointer-to-member-function, just learn the syntax. It's not that hard. – Pete Becker Feb 04 '21 at 18:47
  • @PeteBecker help the person and write the syntax then? Anyway, OP did not provide the full code but I'll assume the functions inside EventHandlers are not static members. I would change them to be static and change `void (EventHandlers::*callback)(void)` to `void (*callback)(void)` and he'd just be able to call it with `taskScheduler.callback()`. I don't know what he's trying to do without more context but it seems to me that he may not need instances of the EventHandlers object. – Leonardo G Feb 04 '21 at 19:10
  • @LeonardoG -- the correct syntax is in the linked answer. Repeating it would be a waste of space. That's why questions get closed. – Pete Becker Feb 04 '21 at 19:23
  • @PeteBecker have you tried compiling OP's code with `(taskScheduler.*callback)();`? It will output an error saying callback is not defined because the actual type of callback is `void (EventHandlers::*TaskScheduler::callback)()`. It's not as trivial as you make it out to be and OP's question is not solved. – Leonardo G Feb 04 '21 at 20:00
  • @LeonardoG — no, I’ve only looked at this question, not at other questions that might be asked. – Pete Becker Feb 04 '21 at 21:25
  • @PeteBecker the error is specifically related to this question. `(taskScheduler.*callback)()` is not the correct syntax in this scenario and this question is falsely marked as a duplicate. It would be the correct syntax if we wanted to directly point to a member function, but not in this case, where we want to point to a pointer to a member function. The problem is you can't access 'callback' in this manner from the taskScheduler object. I honestly don't know a syntax that would work in this scenario. – Leonardo G Feb 05 '21 at 00:07
  • Hello Thanks for your suggestion. I am able to resolved it. – Vaibhav Feb 06 '21 at 10:57

0 Answers0