Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error LNK2001: unresolved external symbol C++

IN my VC++ code which was compiling fine earlier, I have added a function X() like this:

In the file BaseCollection.h
class Base
{
// code
virtual HRESULT X();
//code
};


IN the file DerivedCollection.h
class Derived:public Base
{
    HRESULT X();

}

In the file DerivedCollection.cpp
HRESULT Derived::X
{
// definition of Derived here. 
}

Have included the header files also properly in the .cpp file. But still I don't understand for what reason I am getting the link error:

error LNK2001: unresolved external symbol "public: virtual long __thiscall Base::X()" (?X@Base@@UAEJI@Z)

I am really trying hard to fix this bug. Can anyone kindly help me in getting this issue resolved. Thanks a lot in advance.

like image 663
codeLover Avatar asked Apr 23 '12 11:04

codeLover


2 Answers

Have you implemented X() in Base? You need to do that, or make it pure virtual:

class Base
{
// code
virtual HRESULT X() = 0; //pure virtual. Base doesn't need to implement it.
//code
};

Also, your definition of X() in Derived looks wrong. You probable need something like this:

HRESULT Derived::X()
{
// definition of Derived here. 
}
like image 152
juanchopanza Avatar answered Oct 16 '22 04:10

juanchopanza


You're never defining the function X:

HRESULT Base::X()
{
// definition of X 
}

You'll also need a definition for Derived::X() since that too is virtual.

like image 39
Luchian Grigore Avatar answered Oct 16 '22 03:10

Luchian Grigore