#include <iostream>
using namespace std;
class B
{
public:
int getMsg(int i)
{
return i + 1;
}
};
class A
{
B b;
public:
void run()
{
taunt(b.getMsg);
}
void taunt(int (*msg)(int))
{
cout << (*msg)(1) << endl;
}
};
int main()
{
A a;
a.run();
}
上面的代码在类A中有一个类B,而类A有一个将函数作为参数的方法taunt。 B类的getMsg传入嘲讽...上述代码生成以下错误消息:“错误:没有匹配函数调用'A :: taunt()'”C++:使用函数指针时的无法解析的重载函数
什么导致上述代码中的错误消息?我错过了什么吗?
更新:
#include <iostream>
using namespace std;
class B
{
public:
int getMsg(int i)
{
return i + 1;
}
};
class A
{
B b;
public:
void run()
{
taunt(b.getMsg);
}
void taunt(int (B::*msg)(int))
{
cout << (*msg)(1) << endl;
}
};
int main()
{
A a;
a.run();
}
t.cpp:在构件函数 'void A ::运行()': 第19行:错误:调用没有匹配的函数 'A ::嘲讽()'由于 - 重大错误,编译终止了 。
改变(* MSG)(INT)后,我仍然得到同样的错误(B :: * MSG)(INT)
不能像C++那样使用成员函数指针。尝试使用谷歌搜索,有很多关于它的文章。 – mfontanini
在一个地方,你传递一个'int',期望一个'int(*)(int)',另一个你传递一个'int(B :: *)(int)',其中一个int (*)(int)'是预期的;你为什么希望这个工作? – ildjarn
我刚刚注意到了......我只是修复了它 – user52343