Monday 8 July 2019

C++ template, linking error




I have a problem in calling a template class I have.
I declared a new type name Array, which is a template;



In the .hpp file:



template 
class Array
{
public:

Array();
};


In the .cpp file:



template 
Array::Array()
{
//Do something

}


In main:



Array arr;


I get Linkage error: unresolved external symbol to the ctor.




Any Idea?


Answer



Template functions, including member functions, must be written entirely in the header files. This means that if you have a template class, its implementation must be entirely in a header file. This is because the compiler needs to have access to the entire template definition (not just the signature) in order to generate code for each instantiation of the template.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...