构造函数里添加模板参数.该怎么解决
构造函数里添加模板参数.首先类本身就是带模板的, 例如:C/C++ codetemplateint Nclass Box {}然后需要添
构造函数里添加模板参数.
首先类本身就是带模板的, 例如:
C/C++ codetemplate<int N>class Box {}
然后需要添加一个带模板参数的构造函数, 例如:
C/C++ codetemplate<int N>class Box {public: template<typename I> Box(I begin, I end);}
这都没问题.
接着在类定义外实现构造函数, 例如:
C/C++ codetemplate<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++ codetemplate<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"模板类里面的模板成员函数"