下面的代码有什么问题? This SO question对我没有帮助。
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!!
主要.cpp:
#include "exts.h"
//...
MyClass<double> a; //...
MyClass<int> b(a);
我在 VS2012++ 下得到的错误是:
error C3190: 'MyClass::MyClass(const MyClass)' with the provided template arguments is not the explicit instantiation of any member function of 'MyClass'
在 g++ 下是:
exts.cpp:18:10: error: template-id ‘MyClass<>’ for ‘MyClass::MyClass(MyClass)’ does not match any template declaration template MyClass::MyClass(const MyClass);
最佳答案
替换
template MyClass<double>::MyClass(const MyClass<int>); //instantiation of specifized class member? ERROR here!!
与
template MyClass<double>::MyClass(const MyClass<int>&); //instantiation of specifized class member? ERROR here!!
关于c++ - 模板类的模板构造函数的实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20077004/