0

I am getting linker errors and can not make out the source of the problem. I hope you can help me! The error occurs already when creating an object of the class ClMyClass.

The two errors:

Error   18  error LNK2019: unresolved external symbol "public: virtual __thiscall ClMyClass::~ClMyClass(void)" (??1ClMyClass@@UAE@XZ) referenced in function "void __cdecl Test1(void)" (?Test1@@YAXXZ)

Error   19  error LNK2019: unresolved external symbol "public: __thiscall ClMyClass::ClMyClass(void)" (??0ClMyClass@@QAE@XZ) referenced in function "void __cdecl Test1(void)" (?Test1@@YAXXZ)  


//Test.cpp
#include "StdAfx.h"
#include "MyClass.hpp"

void Test1(){

 ClMyClass oHandle;
}

//ClMyClass.hpp

class ClMyClass: public ClMyClassBase
{
public: 
    ClMyClass(void);
    virtual ~ClMyClass(void);

//methods...

}


//ClMyClass.cpp

ClMyClass::ClMyClass(void):ClMyClassBase(),m_nCallbackError(ERROR_NOT_INITIALIZED), m_eState(eUnknown)
{   
    //initialize members
}

ClMyClass::~ClMyClass(void)
{
    Unassign();
}

EDIT: Added .cpp file

tzippy
  • 6,458
  • 30
  • 82
  • 151
  • 2
    You need to define the two functions your compiler complains about : `ClMyClass` and `~ClMyClass`. Declaring them without implementing them usually leads to that kind of error. – Nbr44 Jul 29 '13 at 13:39
  • @Nbr44 But those are the standard constructor and deconstructor. I dont think they need definition? – tzippy Jul 29 '13 at 13:43
  • 1
    @Borgleader That's a pretty poor duplicate. Next time include \[[tag:c++-faq]\] in your search criteria? – sehe Jul 29 '13 at 13:45
  • @sehe I used the related questions list, I didn't search – Borgleader Jul 29 '13 at 13:45
  • 1
    @tzippy from the moment you wrote their prototype they stopped being the "standard" ctor and dtor. The default ones will only be there if you _don't_ declare them. If you do, you have the responsibility of defining them as well. – Nbr44 Jul 29 '13 at 13:46
  • I'm sorry, I forgo tto include the .cpp implementation. The constr and deconstr are both defined there. Those are classes I did not write. – tzippy Jul 29 '13 at 13:48

2 Answers2

1

You haven't defined the constructor and destructor that you declared in your class ClMyClass.

ClMyClass::ClMyClass(){
    //implementation
}

ClMyClass::~ClMyClass(){
    //implementation
}

That's usually done in a separate .cpp file that contains all the class' member functions' implementations.

JBL
  • 12,588
  • 4
  • 53
  • 84
  • I'm sorry, I completely forgot to include the .cpp file. It exists and is has the definitions of the constructor and deconstructor! – tzippy Jul 29 '13 at 13:46
  • @tzippy What your linker is saying there is "I know that there is two function (ctor and dtor), they've been declared, but I can't find their implementations" – JBL Jul 29 '13 at 13:50
  • Sorry if I was unclear, but they actually are defined in the cpp file. Added it just now to my post. – tzippy Jul 29 '13 at 13:52
0

In your implementation you have not implement the virtual functions. If you use virtual functions, then you must implement all functions which are virtual, even if you don't use them. Otherwise the linker will have the unresolved symbol error you are seeing.

If a function is not used in a class, then will not get an error, because there is no reference to this function. Thus the linker will not see it and doesn't complain. For a virtual function this is different, because they are always generated.

Devolus
  • 21,661
  • 13
  • 66
  • 113
  • Actually, there is an imlementation file. I added it to my post. Sorry. But I still get the error of course. – tzippy Jul 29 '13 at 14:00