【问题标题】:Overloading the increment operator for an iterator class重载迭代器类的增量运算符
【发布时间】:2015-12-03 23:17:51
【问题描述】:

我一直在尝试重载 ++ 运算符以在列表中移动迭代器,但我不断收到错误 C2460 'List::Iterator::++': uses '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);

}; /** 结束 List 类声明 **/

【问题讨论】:

  • 您的 operator++operator--operator* 缺少退货声明。并且缺少参数列表。他们也都有错误的返回类型
  • 如果我包含 return 语句,我会收到语法错误:return。我确实将返回类型更改为 Iterator&
  • 您也到处都缺少/添加分号。您的 return 语句需要它们,而您的方法不需要尾随一个(即{ ... };
  • 你需要一个 return 语句,并且修复你的语法错误。

标签: c++ linked-list operator-overloading increment


【解决方案1】:

以下方法定义格式错误:

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

它们也都声明它们返回一个值,但没有一个函数体这样做。在此代码生效之前,这里还需要做很多工作。

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 1970-01-01
    • 2011-05-18
    • 2020-10-14
    • 2011-10-18
    • 2020-06-14
    • 1970-01-01
    • 2021-07-09
    • 2011-12-07
    相关资源
    最近更新 更多