构造函数问题

[复制链接]
1667|1
 楼主| dfsa 发表于 2012-3-22 18:22 | 显示全部楼层 |阅读模式
首先类本身就是带模板的, 例如:  
template<int N>
class Box {}
然后需要添加一个带模板参数的构造函数, 例如:
template<int N>
class Box {
public:
    template
<typename I> Box(I begin, I end);
}

这都没问题.  
接着在类定义外实现构造函数, 例如:
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

即使换成下面这样也不行:
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>'

有没有什么方法可以单独为构造函数添加迭代器参数, 难道非得在类模板定义里添加之.
pkat 发表于 2012-3-22 18:38 | 显示全部楼层
template<int N>
template<typename I>
Box<N>::Box(I begin, I end)
{
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

282

主题

2404

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部