读书人

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

发布时间: 2012-03-26 15:46:55 作者: rapoo

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

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"模板类里面的模板成员函数"

读书人网 >C++

热点推荐