c++ - 模板类的模板构造函数的实例化

标签 c++ templates instantiation member

下面的代码有什么问题? 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/

相关文章:

c++ - 继承和CRTP

c++ - boolean 运算符++和——

c++ - 从 QMetaObject 检索 QMetaType

jquery - 如何删除 $.get 以获取 Mustache.js 模板

创建包含其他对象的对象列表时出现的 Python 基本问题

c++ - 为什么要对地址运算符和引用声明符共享与符号 (&)?

c++ - 什么是 "Expression SFINAE"?

c++ - 为什么使用局部结构作为 STL 函数参数的代码不能在 g++ 中编译?

Android SDK 错误 : Trying instantiate a class that is not a fragment

swift - 只能返回,不能赋值,Self?