模板类&运算符重载
我该如何重载如下所示的模板类?
template <class T>
const_iterator& List<T>::const_iterator::operator++()
{
current = current->next;
return *this;
}
template <class T>
const_iterator List<T>::const_iterator::operator++(int)
{
const_iterator old = *this;
++( *this );
return old;
}
我收到如下错误:
List.cpp:17: error: expected constructor, destructor, or type conversion before ‘&’ token
List.cpp:23: error: expected constructor, destructor, or type conversion before ‘List’
List.cpp:30: error: expected constructor, destructor, or type conversion before ‘&’ token
List.cpp:35: error: expected constructor, destructor, or type conversion before ‘List’
How do I go about overloading a template class like below?
template <class T>
const_iterator& List<T>::const_iterator::operator++()
{
current = current->next;
return *this;
}
template <class T>
const_iterator List<T>::const_iterator::operator++(int)
{
const_iterator old = *this;
++( *this );
return old;
}
I am getting errors like below:
List.cpp:17: error: expected constructor, destructor, or type conversion before ‘&’ token
List.cpp:23: error: expected constructor, destructor, or type conversion before ‘List’
List.cpp:30: error: expected constructor, destructor, or type conversion before ‘&’ token
List.cpp:35: error: expected constructor, destructor, or type conversion before ‘List’
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
指定返回类型时,您并不在所谓的
List
词法范围内。由于封闭范围内没有类型 const_iterator ,因此您会收到错误(尽管该错误可能会表现得更好一点,恕我直言)。C++11 的另一个选择可能是尾随返回类型:
但是,最好的想法是在类本身中内联定义这些内容。您对类外定义有什么特殊原因吗?
At the time the return type is specified, you're not inside the so-called lexical scope of
List<T>
. And since there is no typeconst_iterator
in the enclosing scope, you get an error (though that one could manifest itself a little bit better, IMHO).Another option for C++11 might be a trailing return type:
However, the best idea would be to just define these things inline in the class itself. Do you have a special reason for the out-of-class definitions?