重载迭代器类的增量运算符
Overloading the increment operator for an iterator class
我一直试图重载++运算符,以在列表中移动迭代器,但我一直收到错误C2460"list::iterator::++":使用"list::iterator"
template <typename E>
class List : public SLinkedList<E> {
public:
// NOTE THE DIFFERENT LETTER – IT IS ONLY USED HERE!
// Use E everywhere else! m
// For a nested class, methods are declared and defined *INSIDE*
// the class declaration.
template <typename I>
class Iterator {
public:
// Give List access to Iterator private fields.
friend class List<E>;
// These are the minimum methods needed.
E operator* {nodePosition->elem}; //dereference the iterator and return a value
Iterator<E> operator++ {nodePosition = nodePosition->next}; //increment the iterator
Iterator<E> operator-- {
nodePosition = nodePosition->prev;
} //decrement the iterator
bool operator==(const Iterator<E> p) {
return (nodePosition == p)
} //test equality of iterators
bool operator!=(const Iterator<E> p) {
return (nodePosition != p)
} //test inequality of iterators
private:
// Constructors & destructor here since only want List class to access.
// List constructor called from List::begin(). Use initializer list or
// create class copy constructor and assignment overload.
Iterator(const List<E>* sl) : llist(sl) {
nodePosition = sl->head;
}
// Class fields.
const List<E>* llist; //give Iterator class a handle to the list
Node<E>* nodePosition; //abstracted position is a pointer to a node
}; /** end Iterator class **/
/* The Iterator class is now fully defined. The rest of these
statements must go AFTER the Iterator class or the compiler
won’t have complete information about their data types.
*/
// REQUIRED: While not necessary for the code to work, my test suite needs
// this defined. Create a less cumbersome name for Iterator<E>. Use
// anywhere you would have used List<E>::Iterator<E> in class List. Allows
// this syntax in main() -- List<int>::iterator instead of List<int>::Iterator<int>.
typedef typename List<E>::Iterator<E> iterator;
/*** All method declarations and fields for the List class go here.
Any method that returns an iterator must be defined here.
***/
iterator begin() const { //return an iterator of beginning of list
// Call iterator constructor with pointer to List that begin() was
// called with.
return iterator(this);
}
E back();
E pop_back();
void push_back(const E e);
};/**end List类声明**/
以下方法定义格式错误:
E operator* {nodePosition->elem}; //dereference the iterator and return a value
Iterator<E> operator++ {nodePosition = nodePosition->next}; //increment the iterator
Iterator<E> operator-- {
nodePosition = nodePosition->prev;
} //decrement the iterator
尽管这些方法都不需要参数,但仍然需要()
。编译器可能将它们视为某种类型的变量定义,并让事情持续足够长的时间,以获得OP正在报告的错误消息。
E operator*() {nodePosition->elem}; //dereference the iterator and return a value
Iterator<E> operator++() {nodePosition = nodePosition->next}; //increment the iterator
Iterator<E> operator--() {
nodePosition = nodePosition->prev;
} //decrement the iterator
它们也都是返回值的状态,但没有一个函数体这样做。在这段代码发挥作用之前,还需要做更多的工作。
相关文章:
- 迭代器类的重载前缀增量运算符会引发分段错误
- 了解运算符重载和迭代器,为什么它会打印出"wrhrwwr"?
- 映射迭代器与运算符不匹配
- 提供运算符+或运算符到双向迭代器有什么缺点吗?
- 无法使用迭代器查找类对象。(二进制 '==': 'userInfo' 不定义此运算符)
- 迭代器和无符号整数的重载 + 运算符
- 迭代器的范围 TS 和 C++20 概念是否需要能够使用"运算符>"?
- C++ 使用矢量迭代器时的运算符优先级
- 在 c++ 迭代器中,我应该同时定义运算符== 和运算符!=吗?
- 迭代器关系运算符出错(带单独链接和迭代器的自定义哈希表)
- 为什么 std::list 迭代器没有运算符+
- 如何让迭代器使用赋值运算符对列表进行操作
- 迭代器和增量运算符
- 为什么我们要把 :: (范围重新定位运算符)放在迭代器之前
- Gnu 的 <Iterator>reverse_iterator::运算符>和代理迭代器
- 输入迭代器 - 星号和后缀运算符
- 后缀运算符链表泛型节点迭代器
- 在自定义双链接列表中的自定义迭代器的取消运算符,找不到二进制操作员
- 与运算符"!="不匹配(c++ 迭代器)
- 在地图迭代器上不支持非仿制地图上的平等运算符(==)