Jastria Rahmat
Jastria Rahmat

Reputation: 905

C++ Function Callback using class member and run it in main

I know, many post asking about this, been reading about 10 post with long answers but, every post seems different case and needs different implementation.

so, what did i do wrong here?

#include <iostream>
using namespace std;
class Foo {
   public:
    void callbackFunc(void (*funcParam1)());
    void funcA();

};
void Foo::callbackFunc(void (*funcParam1)()) {
    funcParam1();
}
void Foo::funcA() { cout << "func A OK.." << endl; }
void aa(){
    int x =0;
}
Foo bar;
int main() { bar.callbackFunc(bar.funcA); }


this example throws error: invalid use of non-static member function ‘void Foo::funcA()’ in the main() especially in the callbackFuncparameter.

i want to use it in arduino later.

Upvotes: 1

Views: 47

Answers (1)

tdao
tdao

Reputation: 17668

You may get away with the error with static funcA:

static void funcA();

Looks like the problem is that your callbackFunc expects a function pointer, but member function funcA is not a normal function and expects an object to begin with. Making it static would get away of the issue.

Upvotes: 2

Related Questions