Rollen Holt
Rollen Holt

Reputation: 517

error LNK2019: unresolved external symbol

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

but when I link it:

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)

can you tell me why?

Upvotes: 0

Views: 10419

Answers (2)

Collin
Collin

Reputation: 12267

A::function1, A::function and B::function2 are all declared, but never defined. You can't get a pointer to the function if it is not defined, where would it point?

Upvotes: 1

Carl Norum
Carl Norum

Reputation: 224844

You haven't implemented A::function(), A::function1(), or B::function2(). You need to do that.

Upvotes: 2

Related Questions