打印

构造函数问题

[复制链接]
938|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

粉丝