You are, of course correct. I should've looked at my own code from
previous class definitions and seen that.
Thanks very much.
On Mon, 08 Oct 2007 14:48:08 -0400, Dave Cullen <nospam@mail.com> wrote:
I have an abstract class defined in a header file, and have derived my
own class from it overriding the pure virtual methods. The .cpp's
compile OK but the linker gives unresolved external symbol errors when
trying to link my methods.
// the abstract class, in abstract.h
class CExchange
{
public:
virtual short Exchange(BYTE *pabExchangeBuffer,
DWORD eBufferSize,
DWORD eNumberOfBytesToSend,
DWORD &reNumberOfBytesReceived) = 0;
virtual ~CExchange(void)
{
}
};
/////////////////
// My derived class definition in Exchange.h
#include "abstract.h"
class C_Exchange : virtual public CExchange
{
public:
virtual short Exchange(BYTE *pabExchangeBuffer,
DWORD eBufferSize,
DWORD eNumberOfBytesToSend,
DWORD &reNumberOfBytesReceived);
};
////////////////////
// The derived class implimentation in Exchange.cpp
#include "abstract.h"
class C_Exchange : virtual public CExchange
{
public:
virtual short Exchange(BYTE *pabExchangeBuffer,
DWORD eBufferSize,
DWORD eNumberOfBytesToSend,
DWORD &reNumberOfBytesReceived)
{
short ret = 0;
return ret;
}
};
///////////////
// And the final use in main()
#include "exchange.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
C_Exchange* ex = new C_Exchange();
return 0;
}
It all works until I try to instantiate an object of C_Exchange, at
which the linker says:
error LNK2001: unresolved external symbol "public: virtual short
__thiscall C_Exchange::Exchange(unsigned char *,unsigned long,unsigned
long,unsigned long &)" (?Exchange@C_Exchange@@UAEFPAEKKAAK@Z)
Also, if I change the include from abstract.h to exchange.h in the cpp
the compiler says I have duplicate class definition.
Unlike namespaces, you can't "reopen" class definitions. There should be
one and only one class definition.
I always define a
class in a header and implement it in a cpp without problems, why would
this one be any different?
You're violating the "one definition rule", and it's just "luck" that it
ever worked under any conditions. Here is the corrected version of
Exchange.cpp:
// The derived class implementation in Exchange.cpp
#include "Exchange.h"
short
C_Exchange::Exchange(
BYTE *pabExchangeBuffer,
DWORD eBufferSize,
DWORD eNumberOfBytesToSend,
DWORD &reNumberOfBytesReceived)
{
short ret = 0;
return ret;
}
--
Doug Harrison
Visual C++ MVP