簡體   English   中英

重載迭代器類的增量運算符

[英]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);

}; / **結束列表類聲明** /

以下方法定義格式不正確:

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

它們也都聲明它們返回一個值,但是所有函數體都沒有。 在此代碼起作用之前,這里需要做很多工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM