首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

构造函数里添加模板参数.该怎么解决

2012-03-26 
构造函数里添加模板参数.首先类本身就是带模板的, 例如:C/C++ codetemplateint Nclass Box {}然后需要添

构造函数里添加模板参数.
首先类本身就是带模板的, 例如: 

C/C++ code
template<int N>class Box {}

然后需要添加一个带模板参数的构造函数, 例如: 
C/C++ code
template<int N>class Box {public:    template<typename I> Box(I begin, I end);}

这都没问题. 
接着在类定义外实现构造函数, 例如: 
C/C++ code
template<int N, typename I>Box<N>::Box<I>(I begin, I end) {    //...实现代码}

编译出现下列错误: 
error: 'Box<N>::Box' names the constructor, not the type
error: and 'Box<N>' has no template constructors

即使换成下面这样也不行: 
C/C++ code
template<int N, typename I>Box<N>::Box(I begin, I end) {    //...实现代码}

编译出现下列错误: 
error: invalid use of incomplete type 'class Box<N>'
error: declaration of 'class Box<N>'

有没有什么方法可以单独为构造函数添加迭代器参数, 难道非得在类模板定义里添加之.

[解决办法]
template<int N>
template<typename I>
Box<N>::Box(I begin, I end)
{
}

[解决办法]
楼上正解
[解决办法]
goole"模板类里面的模板成员函数"

热点排行