"*"标记之前的预期非限定 id:指向成员函数的指针

Expected unqualified-id before '*' token: Pointer to Member Functions

本文关键字:id 指针 函数 成员      更新时间:2023-10-16

我正在尝试运行此代码,但无法理解是什么导致了错误。

#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;
}