Nic*_*oft 2 c++ parameters class function parameter-passing
所以我试图在 C++ 中将一个函数作为参数传递给我的两个类:
void class1::func_1(QString str)
{
class2* r = new class2();
r->func_2(str, &process);
}
void class1::process(QString str)
{
//Do something with str
}
Run Code Online (Sandbox Code Playgroud)
其中 'r->func_2' 如下所示:
QString class2::func_2(QString str, void (*callback)(QString))
{
//Do something else
}
Run Code Online (Sandbox Code Playgroud)
然而,当我尝试编译时,我收到以下错误:
must explicitly qualify name of member function when taking its address
r->func_2(str, &process);
^~~~~~~~
class1::
cannot initialize a parameter of type 'void (*)(QString)' with an rvalue of type 'void (class1::*)(QString)'
r->func_2(str, &process);
^~~~~~~~
Run Code Online (Sandbox Code Playgroud)
我不明白为什么。有任何想法吗?我显然做错了什么......只是不确定是什么。任何帮助将不胜感激。谢谢!
小智 5
您需要在传递其地址时完全限定成员函数名称:
void class1::func_1(QString str)
{
class2* r = new class2();
r->func_2(str, &class1::process);
}
Run Code Online (Sandbox Code Playgroud)