我正在尝试运行此代码,但无法理解导致错误的原因。
#include <iostream>
using namespace std;
class Shape
{
public:
void show ( float ) { cout << "Hello"; }
};
int main ( )
{
void (Shape::*FPtr2) (float) = &Shape :: show;
(Shape::*FPtr2)(1.1);
return 0;
}
最佳答案
调用非静态成员函数需要一个对象。通过指向成员函数的指针调用非静态成员函数也需要一个对象。
#include <iostream>
using namespace std;
class Shape
{
public:
void show ( float ) { cout << "Hello"; }
};
int main ( )
{
void (Shape::*FPtr2) (float) = &Shape :: show;
Shape myShape; // here is my object
(myShape.*FPtr2)(1.1); // here is the call to the object's show function via pointer
return 0;
}
关于c++ - '*' token : Pointer to Member Functions 之前的预期不合格 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26048053/