3

以下问题让我发疯,尽管它看起来并不奇怪:

class Foo;

// This is the location of the first error code
//        ↓
int (Foo::*)(int) getPointer()
{
    return 0;
}

海湾合作委员会给了我:

error: expected unqualified-id before ')' token
error: expected initializer before 'getPointer'

PS:我用 -std=c++11 编译

4

3 回答 3

4
int ( Foo::* ( getPointer() ) )();

话虽如此,请记住您可以使用typedef. 对于函数指针,它通常会提高整体可读性:

typedef int ( Foo::* TypeName )();

TypeName getPointer();
于 2013-11-13T19:03:10.727 回答
0

使用 typedef,例如:

class Foo;

typedef int (Foo::*POINTER)(int);

POINTER getPointer()
{
    return 0;
}

这里有更多的推理:http: //www.parashift.com/c++-faq/typedef-for-ptr-to-memfn.html

于 2013-11-13T19:02:31.020 回答
0

看起来您正在尝试使用函数指针,但没有给它命名:P

用这个:

int (Foo::*myPointer)(int) getPointer()
{
    return 0;
}
于 2013-11-13T19:04:43.537 回答