开发者

iterator problem

The code is as follows

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;}

            // d开发者_运维百科ereferencing 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'


Looking over your code it appears that you've got the right kinds of ideas by including those typedefs with your own iterator implementation. However, why go to all the trouble in the first place when something else could do it for you? Have you looked at iterator_traits or the standard iterator? They just add typedefs to your code which will aid you in developing new iterator types.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜