Edit: This is not a duplicate of the linked question since I am using explicit instantiation and only a specific type of member functions do not link (others do
You should use the explicit instantion of the method template<typename T> template<typename U> Vector<T> Vector<T>::operator+(const Vector<U> & other) const
(for all possible pairs of T
and U
) in addition to the explicit instantion of the Vector<T>
class:
template Vector<int> Vector<int>::operator+(const Vector<short> & other) const;
Also you may simply move the definition of the Vector<T>::operator+
method to the header file.
In C++11 the extern template
directive was introduced. You may use it in the header file for Vector<T>
class (as @StoryTeller suggested in the comments):
extern template struct Vector<int>;
...to prevent the compiler from instantiating the Vector<T>
class in every translation unit its specializations are used. Of course the same extern template
directives may also be used for all Vector<T>::operator+
specializations explicitly instantiated in the .cpp
file.