0

I know this problem may occur when I declare function and not define it. But this is not the case.

//In the 'H' file. Template class.
BSNode(T data);


\\In the cpp definitions

template <class T>
BSNode<T>::BSNode(T data)
{
    _root = this;
    _data = data;
    _right = NULL;
    _left = NULL;
}

And still, I get the next Error:

Error   1   error LNK2019: unresolved external symbol "public: __thiscall BSNode<int>::BSNode<int>(int)" (??0?$BSNode@H@@QAE@H@Z) referenced in function _main  c:\Users\a\OneDrive\ss\visual studio 2013\Projects\Project5\2\Source.obj    2_TREES

What also may cause this Error to occur if its not the incompatibility between the 'H' and the 'cpp' files?

dfsfg sfg
  • 77
  • 1
  • 9

1 Answers1

0

The code that uses this constructor with the int instantiation should also be in the .cpp file. The compiler needs it to generate the instance of the code, because it cannot generate all possible instances.

Several solutions:

  • move at least one use of the constructor with the int instance in the cpp file
  • rename the .cpp file into a .template file that you can include in every file that uses the BSNode instances.
  • add template class BSNode<int>; in the .cpp file to force the template instantiation.
Franck
  • 1,635
  • 1
  • 8
  • 11