我在使用C++函数指针时遇到问题.这是我的例子:
#include <iostream>
using namespace std;
class bar
{
public:
void (*funcP)();
};
class foo
{
public:
bar myBar;
void hello(){cout << "hello" << endl;};
};
void byebye()
{
cout << "bye" << endl;
}
int main()
{
foo testFoo;
testFoo.myBar.funcP = &byebye; //OK
testFoo.myBar.funcP = &testFoo.hello; //ERROR
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Compilator在testFoo.myBar.funcP = &testFoo.hello;以下位置返回错误:
ISO C++禁止获取绑定成员函数的地址以形成指向成员函数的指针.说'&foo :: hello'
无法在赋值时将'void(foo :: )()'转换为'void()()'
所以我试着这样:
class bar
{
public:
void (*foo::funcP)();
};
Run Code Online (Sandbox Code Playgroud)
但是现在编译器增加了一个:
'foo'尚未宣布
有没有办法让它发挥作用?
提前感谢您的建议