错误LNK2019:未解析的外部符号

时间:2012-03-25 04:44:01

标签: c++ visual-c++

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

但是当我链接它时:

1>c.obj : error LNK2019: unresolved external symbol "public: bool __thiscall A::function1(void)const " (?function1@A@@QBE_NXZ) referenced in function _wmain
1>c.obj : error LNK2019: unresolved external symbol "public: void __thiscall A::function(int)" (?function@A@@QAEXH@Z) referenced in function _wmain
1>c.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall B::function2(void)const " (?function2@B@@UBE_NXZ)
你可以告诉我为什么吗?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

A::function1A::functionB::function2都已声明,但从未定义过。如果未定义函数,则无法获得指向该函数的指针,它指向何处?