简体   繁体   English

实例化模板类的模板构造函数

[英]instantiation of a template constructor of template class

What's wrong with the following code? 以下代码有什么问题? This SO question doesn't help me. 这个问题对我没有帮助。

exts.h: exts.h:

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

exts.cpp: 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: main.cpp中:

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

The error I get under VS2012++ at the line noted is: 我注意到的VS2012 ++下的错误是:

error C3190: 'MyClass::MyClass(const MyClass)' with the provided template arguments is not the explicit instantiation of any member function of 'MyClass' 错误C3190:带有提供的模板参数的'MyClass :: MyClass(const MyClass)'不是'MyClass'的任何成员函数的显式实例化

And under g++ is: 而在g ++下是:

exts.cpp:18:10: error: template-id 'MyClass<>' for 'MyClass::MyClass(MyClass)' does not match any template declaration template MyClass::MyClass(const MyClass); exts.cpp:18:10:error:'MyClass :: MyClass(MyClass)'的模板ID'MyClass <>'与任何模板声明模板MyClass :: MyClass(const MyClass)都不匹配;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM