【问题标题】:instantiation of a template constructor of template class模板类的模板构造函数的实例化
【发布时间】:2013-11-19 16:36:29
【问题描述】:

以下代码有什么问题? 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!!

main.cpp:

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

我在 VS2012++ 下遇到的错误是:

错误 C3190: 'MyClass::MyClass(const MyClass)' 与 提供的模板参数不是任何的显式实例化 'MyClass' 的成员函数

而在g++下是:

exts.cpp:18:10: 错误:模板 ID ‘MyClass’ for ‘MyClass::MyClass(MyClass)’ 不匹配任何模板 声明模板 MyClass::MyClass(const MyClass);

【问题讨论】:

  • 错误信息是什么?

标签: c++ templates instantiation member


【解决方案1】:

替换

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!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    相关资源
    最近更新 更多