2012-03-25 100 views
0
#include <iostream> 
using namespace std; 

class A { 
public: 
    void function(int num); 
    bool function1()const; 
    virtual bool function2() const=0; 
}; 

class B:public A { 
public : 
    bool function2()const; 
}; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    void (A::* p)(int)= &A::function; //不是地址,而是一个指向成员函数的指针 
    // Edit: Google translation of the above comment is 
    // "Not address, but a pointer to a member function pointer" 

    bool (A::* p1)()const =&A::function1; // 指向成员函数的指针可以指向一个常量成员函数 
    // Edit: Google translation of the above comment is 
    // "Point to a member function pointer can point to a const member function" 

    B b; 
    A *a=&b; 
    (a->*p1)(); 
    (b.*p1)(); 

    return 0; 
} 

但是当我将其链接:错误LNK2019:解析外部符号

1>c.obj : error LNK2019: unresolved external symbol "public: bool __thiscall A::function1(void)const " ([email protected]@@QBE_NXZ) referenced in function _wmain 
1>c.obj : error LNK2019: unresolved external symbol "public: void __thiscall A::function(int)" ([email protected]@@[email protected]) referenced in function _wmain 
1>c.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall B::function2(void)const " ([email protected]@@UBE_NXZ) 

你能告诉我为什么吗?

+2

你正在编码没有缩进。这很难阅读。 – kev 2012-03-25 04:49:31

+1

我使用'astyle'缩进了代码。我也使用Google翻译了这些评论,但他们似乎与这个问题无关。 – 2012-03-25 04:58:50

回答

2

您尚未实施A::function(),A::function1()B::function2()。你需要这样做。

+0

他怎么能在不知道这一点的情况下实现继承的复杂性?我会猜测他没有写这个代码? – Aerovistae 2012-03-25 04:55:18

+0

我不确定我是否理解这个问题,但通常会出现像这样的问题,因为人们复制/粘贴他们不理解的代码。 – 2012-03-25 04:56:21

+0

@Aerovistae这将是未定义符号的原因。它们或者从未被定义,或者它们驻留的源文件没有被链接。 – Collin 2012-03-25 04:59:32

1

A::function1,A::functionB::function2都被声明,但从未定义过。如果未定义函数,则无法获取指向函数的指针,它将指向何处?

相关问题