[英]How to overload the increment operators in C++ for an Object with 2 variables?
[英]C++/g++ overload increment operators
我正在尝试实现一个双链表,并希望创建一个迭代器。 结构是:
template<class type>
class List {
size_t listElementCnt;
...
public:
...
class iterator {
...
public:
...
iterator& operator ++();
iterator operator ++(int);
...
};
...
};
现在我想实现两个运算符的重载:
template<class type>
typename iterator& List<type>::iterator::operator ++() {
...
}
template<class type>
typename iterator List<type>::iterator::operator ++(int) {
...
}
现在有两个错误:
当我使其他运算符(例如,解引用或(不等于)运算符)过载时,没有错误。 错误只出现在g ++ - 编译器中。 visual c ++的编译器没有显示任何错误,并且在这里正常工作。
在成员函数的离线定义中,该函数的返回类型不在类范围内,因为尚未看到类名。 因此,将您的定义更改为如下所示:
template<class type>
typename List<type>::iterator& List<type>::iterator::operator ++() {
...
}
template<class type>
typename List<type>::iterator List<type>::iterator::operator ++(int) {
...
}
您需要在返回类型中限定iterator
:
template<class type>
typename List<type>::iterator& List<type>::iterator::operator ++() {
...
}
template<class type>
typename List<type>::iterator List<type>::iterator::operator ++(int) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.