Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ template, linking error [duplicate]

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 <typename T> class Array { public:    Array(); }; 

In the .cpp file:

template <typename T> Array<T>::Array() { //Do something } 

In main:

Array<int> arr; 

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

Any Idea?

like image 723
Boris Raznikov Avatar asked Aug 30 '09 14:08

Boris Raznikov


1 Answers

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.

like image 119
Tyler McHenry Avatar answered Sep 20 '22 07:09

Tyler McHenry