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?
You have two levels of templates, and have to specify them separately.
template<typename T>
template<typename S>
X<T>::X(X<S> y){}
Try this:
template<typename T>
template<typename S>
X<T>::X()( X<S> y )
{
}
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){}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With