1

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

Community
  • 1
  • 1
DanielTuzes
  • 2,494
  • 24
  • 40

1 Answers1

3

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!!
navid
  • 566
  • 6
  • 15
Drax
  • 12,682
  • 7
  • 45
  • 85