Does the template constructor in the template class need to be defined in the class definition?

Suppose I write a template class with a template constructor, for example.

template<typename T>
class X{


    template<typename S>
    X(X<S> x){}
};

compiles fine. However, when I try to define a constructor outside the template declaration, for example:

template<typename T>
class X{


    template<typename S>
    X(X<S> x);
};


template<typename T, typename S>
X<T>::X(X<S> y){}

I get the following error:

error: invalid use of incomplete type ‘class X<T>’

why? Can't define a template class constructor for a template outside of a class declaration?

+5
source share
3 answers

You have two levels of templates, and you need to specify them separately.

template<typename T>
template<typename S>
X<T>::X(X<S> y){}
+10

Try the following:

template<typename T>
template<typename S>
X<T>::X()( X<S> y )
{
}
+5

, ,

template<typename T>
template <typename S>
X<T>::X(X<S> y){}
+4

All Articles