2011-02-03 167 views
1

的代碼如下迭代問題

template<class T> 
class arrayList { 
public: 
    // constructor, copy constructor and destructor 
    arrayList(int initialCapacity = 10); 
    arrayList(const arrayList<T>&); 
    ~arrayList() { 
     delete[] element; 
    } 

    class seamlessPointer; 
    seamlessPointer begin() { 
     return seamlessPointer(element); 
    } 
    seamlessPointer end() { 
     return seamlessPointer(element + listSize); 
    } 

    // iterator for arrayList 
     class iterator 
     { 
     public: 
      // typedefs required by C++ for a bidirectional iterator 
      typedef bidirectional_iterator_tag iterator_category; 
      typedef T value_type; 
      typedef ptrdiff_t difference_type; 
      typedef T* pointer; 
      typedef T& reference; 

      // constructor 
      iterator(T* thePosition = 0) {position = thePosition;} 

      // dereferencing operators 
      T& operator*() const {return *position;} 
      T* operator->() const {return position;} 

      // increment 
      iterator& operator++(); 
         {++position; return *this;} 
      iterator operator++(int); 

      // decrement 
      iterator& operator--(); 
      iterator operator--(int) ; 

      // equality testing 
      bool operator!=(const iterator right) ; 
      bool operator==(const iterator right) ; 
     protected: 
      T* position; 
     }; // end of iterator class 

     class seamlessPointer: public arrayList<T>::iterator { 

     public: 
      typedef random_access_iterator_tag iterator_category; 
      typedef T value_type; 
      typedef ptrdiff_t difference_type; 
      typedef T* pointer; 
      typedef T& reference; 
      // constructor 
      seamlessPointer(T *thePosition); 
      seamlessPointer(const seamlessPointer & rhs); 
      //arithmetic operators 
      seamlessPointer operator+(int n) ; 

      seamlessPointer operator-(int n) ; 
    }; 

protected: 
    T* element; // 1D array to hold list elements 
    int arrayLength; // capacity of the 1D array 
    int listSize; // number of elements in list 
}; 

c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algo.h:5250:4: error: no match for 'operator-' in '__last - __first' 
+0

[迭代器問題](http://stackoverflow.com/questions/4882241/iterator-problem) – 2011-02-03 15:56:38

回答

1

尋找你的代碼,看來你已經通過包括你自己的迭代執行這些類型定義得到了正確的種思路。但是,爲什麼在其他事情可以爲你做這件事的時候,首先要解決所有的麻煩?你看過iterator_traits還是標準iterator?他們只是添加typedefs到你的代碼,這將有助於你開發新的迭代器類型。

+0

@ Sean你說得對。從我的答案中刪除了這個,儘管現在我的答案已經不再是一個答案了。 – wheaties 2011-02-03 14:44:16