1

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

skaffman
  • 398,947
  • 96
  • 818
  • 769

3 Answers3

4

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.

Erik
  • 88,732
  • 13
  • 198
  • 189
  • The template class defined in header file is TVec > > and the template class TVec { public: typedef TVal* TIter; And in the .cpp file, the linker error is coming for the line: for (TNdClss::TIter CI = ndClss.BegI(); CI ", CI->Val1); where typedef TVec > > TNdClss; is defined in header. – Somnath Chakrabarti Mar 14 '11 at 02:09
  • 1
    Where's `TLst & TLst::operator=` implemented? – Erik Mar 14 '11 at 09:04
1

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.)

Ken White
  • 123,280
  • 14
  • 225
  • 444
0

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