#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