Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template constructor in template class must be defined in the class definition?

suppose I write a template class with a template constructor, like that.

template<typename T>
class X{


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

compiles fine. However, when I try to define the constructor outside of the template declaration, like this:

template<typename T>
class X{


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


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

I receive the following error:

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

why? Is it not possible to define a template constructor of a template class outside of the class declaration?

like image 380
gexicide Avatar asked Aug 17 '12 15:08

gexicide


3 Answers

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

template<typename T>
template<typename S>
X<T>::X(X<S> y){}
like image 87
Bo Persson Avatar answered Oct 26 '22 23:10

Bo Persson


Try this:

template<typename T>
template<typename S>
X<T>::X()( X<S> y )
{
}
like image 34
Torsten Robitzki Avatar answered Oct 27 '22 00:10

Torsten Robitzki


Your class has a single template parameter, and you essentially have a template function inside of it, so you need

template<typename T>
template <typename S>
X<T>::X(X<S> y){}
like image 24
juanchopanza Avatar answered Oct 27 '22 00:10

juanchopanza