DanielTuzes
DanielTuzes

Reputation: 2744

instantiation of a template constructor of template class

What's wrong with the following code? This SO question doesn't help me.

exts.h:

template <typename T> class MyClass
{
public:
    MyClass();
    MyClass(const MyClass& tocopy);
    template <typename U> MyClass(const MyClass<U>& tocopy);
    // ...
};

exts.cpp:

#include "exts.h"
template <typename T> MyClass<T>::MyClass() {}
template <typename T> MyClass<T>::MyClass(const MyClass& tocopy)
{// ... }

template <typename T> template <typename U> MyClass<T>::MyClass(const MyClass<U>& tocopy)
{// ... }

template class MyClass<int>;    //instantiation of the class
template class MyClass<double>; //instantiation of the class
template MyClass<double>::MyClass(const MyClass<int>); //instantiation of specifized class member? ERROR here!!

main.cpp:

#include "exts.h"
//...
MyClass<double> a; //...
MyClass<int> b(a); 

The error I get under VS2012++ at the line noted is:

error C3190: 'MyClass::MyClass(const MyClass)' with the provided template arguments is not the explicit instantiation of any member function of 'MyClass'

And under g++ is:

exts.cpp:18:10: error: template-id ‘MyClass<>’ for ‘MyClass::MyClass(MyClass)’ does not match any template declaration template MyClass::MyClass(const MyClass);

Upvotes: 1

Views: 1686

Answers (1)

Drax
Drax

Reputation: 13288

Replace

template MyClass<double>::MyClass(const MyClass<int>); //instantiation of specifized class member? ERROR here!!

with

template MyClass<double>::MyClass(const MyClass<int>&); //instantiation of specifized class member? ERROR here!!

Upvotes: 3

Related Questions