- A template function should be declared and implemented in a header file, not in a .cpp file.
-
A template class should be declared and implemented in a header file, not in a .cpp file. That is, member functions and constructors for a template class should be implemented after the class declaration, but in the header file!
-
Outside the class declaration, the name of a template class includes the <T>; e.g., Array<T>
-
A declaration of a template function should be preceded by the declaration of the template parameter name; e.g.,
template <class T>
-
A declaration of a template class should be preceded by the declaration of the template parameter name; e.g.,
template <class T>
-
A template function implementation should be preceded by the declaration of the template parameter name; e.g:
template <class T>
Note this also applies to implementations of member functions of template classes as well as implementations of non-member template functions.
So the implementation of a member function outside the class declaration should use the class scope operator :: with this class name; e.g., the implementation of add in Array<T> should begin:
template <class T> void Array<T>::add(const T& x) { ... }