Search code examples
c++inheritancecompiler-errorslinker-errorslnk2019

c++ (constructor) inheritance - LNK 2019 error


Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

I have a LNK error that involves a class A and its derived class B. More precisely, I have this compilation error

Error   239 error LNK2019: unresolved external symbol "public: virtual __thiscall A::~A(void)" (??1A@@UAE@XZ) referenced in function "public: virtual __thiscall B::~B(void)" (??1B@@UAE@XZ)    D:\Products\path\file.lib(B.obj)
Error   240 error LNK2019: unresolved external symbol "public: __thiscall A::A(void)" (??A@@QAE@XZ) referenced in function "public: __thiscall B::B(void)" (??B@@QAE@XZ)    D:\Products\path\file.lib(B.obj)
Error   241 error LNK2019: unresolved external symbol "public: void __thiscall A::function(float * *,float * *,float * *,float * *,int)" (?function@A@@QAEXPAPAM000H@Z) referenced in function "public: class SomeType* __thiscall B::function_bis(void)" (?function_bis@B@@QAEPAVSomeType@@XZ) D:\Products\path\file.lib(B.obj)

I guess this may be related to, say, call of inherited constructor, or the non-respect of the signature in some call of function() or function_bis(). However, such mistakes i cannot find.

Do you have a hint to a possible way to solve ? Here is code for (simplified) A and B.

B.cpp

B::B(void)
{
}

B::B(Type1* d1, Type1* d2, Type1* r):A()
{
    D1= d1;
    D2= d2;
    R= r;
}


B::~B( void )
{
}

SomeType* B::function()
{
     // do things
     function_bis() ;
}

B.h

class B:
    public A
{
public:

    B(void) ;
    B(Type1* , Type1* , Type1* );
    virtual ~B(void);

    SomeType* function() ;

private:

    Type1* D1;
    Type1* D2;
    Type1* R;

};

A.cpp

using namespace std ;

A::A(void){}

A::~A(void){}

void A::function_bis(float** d, float** d2, float** d3, float** d4, int n)
{}

A.h

class A
{

public:
    A(void);
    virtual ~A(void);

    void function_bis(float** , float** , float** , float** , int );

};

Thanks!


Solution

  • Everything looks legit in your code.

    My guess is that you actually don't compile A.cpp or somehow you don't include the resulting object file in your linking step (you miss A::A, A::~A and A::function_bis which are defined in A.cpp).