[英]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.我知道,很多帖子都在问这个问题,阅读了大约 10 篇回答很长的帖子,但是,每篇帖子似乎都不同,需要不同的实现。
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 callbackFunc
parameter.此示例抛出
error: invalid use of non-static member function 'void Foo::funcA()'
在main()
中error: invalid use of non-static member function 'void Foo::funcA()'
特别是在callbackFunc
参数中。
i want to use it in arduino later.我想稍后在 arduino 中使用它。
You may get away with the error with static funcA
:您可以通过
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.看起来问题是你的
callbackFunc
需要一个函数指针,但成员函数funcA
不是一个普通的函数并且需要一个对象开始。 Making it static
would get away of the issue.将其
static
可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.