Magix
Magix

Reputation: 5349

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 ?

Upvotes: 2

Views: 59

Answers (2)

Sergey Panov
Sergey Panov

Reputation: 1

It is not an answer, but a repeat of this(similar) question -- the class is defined/declared first and compiler is Ok with it:

 template<typename UnsignedInt,
        std::enable_if_t<std::is_unsigned<UnsignedInt>::value, int> = 0>
class binangle {
    ...
    template<typename F>
    void set_from_degrees(F rad);
    ...

yet, when it hist the implementation

template<typename F>
void binangle<UnsignedInt>::set_from_radians(F rad) {
    static_assert(std::is_unsigned<UnsignedInt>::value);
    static_assert(std::is_floating_point<F>::value);
    ...

it throws error:

/home/sipan/src/vgit/signals/tests/../include/binangles_impl.hpp:23:51: error: invalid use of incomplete type ‘class signals::binangle<UnsignedInt>’
   23 | void binangle<UnsignedInt>::set_from_radians(F rad) {
      |                                                   ^

This is as close as you can get to the textbook exaMPLE

//template member definition
template<typename T>
template<typename T2>
void A<T>::f(T2)
{
    // some code
}

but it fails.

Upvotes: 0

P.W
P.W

Reputation: 26800

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) {

}

Upvotes: 3

Related Questions