Search code examples
c++classtemplatesheader-filesgeneric-programming

Template member of template class implementation in header file


I have a class that looks like this

template<class T>
class Matrix {
    ...
    template<class T2> auto dot(Matrix<T2> const& other);
}

Here is my implementation, under the declaration in the header file :

template<class T, class T2>
auto Matrix<T>::dot(Matrix<T2> const& other) {
    [impl]
}

The error I get looks like this :

(C2244) 'Matrix<T>::dot' : unable to match function definition to an existing declaration

Where am I going wrong ?


Solution

  • The syntax is wrong. You have a function template with template parameter T2 within a class template with template parameter T . It has to be defined like this:

    template<class T>
    template<class T2> 
    auto Matrix<T>::dot(Matrix<T2> const& other) {
    
    }