Somnath Chakrabarti
Somnath Chakrabarti

Reputation: 9

What is the meaning of error LNK2019

Can Somebody tell me what does the following error implies?

Error 2 error LNK2019: unresolved external symbol "public: class TLst & __thiscall TLst::operator=(class TLst const &)" (??4?$TLst@VTInt@@@@QAEAAV0@ABV0@@Z) referenced in function "public: void __thiscall TPair >::GetVal(class TInt &,class TLst &)const " (?GetVal@?$TPair@VTInt@@V?$TLst@VTInt@@@@@@QBEXAAVTInt@@AAV?$TLst@VTInt@@@@@Z) randomgraph.obj randomgraph

Upvotes: 1

Views: 904

Answers (3)

Somnath Chakrabarti
Somnath Chakrabarti

Reputation: 9

This problem is solved. In the template class TLst, the function

TLst TLst::operator=(const TLst&);

was declared but it was not defined.

I had to define the function in my .cpp file. I could have defined it in my header file as well.

Thanks for the replies.

Somnath

Upvotes: 0

Erik
Erik

Reputation: 91270

In general, it means that the linker sees a reference to a symbol, but it can't find it anywhere - often due to a missing library or object file.

In this case this happened because you implemented your templated class'es member functions in a .cpp file - they should be implemented in the header.

A template class is a template not a class. When the compiler see you using e.g. vector<int> f; it creates a new class vector<int> from the template vector. In order to create e.g. vector<int>::size() it needs to see the implementation of size() at the point where the template is instantiated - and it can't do that if the implementation of size() isn't in the header file.

You can get around this by explicitly instantiating vector for int - Then the compiler will see the explicit instantiation when it compiles the cpp file. This defeats the purpose of having a template - it'd only be usable for the types you predefine with explicit instantiation. So, short story, always fully implement templates in header files.

Upvotes: 4

Ken White
Ken White

Reputation: 125689

Unresolved external symbol means that there's a reference that the linker can't find. It's usually caused by forgetting to add an object file or library to the link step. (Including the header file for a class isn't enough - you also have to add the implementation code.)

Upvotes: 1

Related Questions