2011-02-03 109 views
1

在arrayList中,我自定义迭代器,然后使用stl和迭代器对字符串(单词)进行排序。迭代器问题

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

    // ADT methods 
    bool empty() const { 
     return listSize == 0; 
    } 
    int size() const { 
     return listSize; 
    } 
    T& get(int theIndex) const; 
    int indexOf(const T& theElement) const; 
    void erase(int theIndex); 
    void insert(int theIndex, const T& theElement); 
    void output(ostream& out) const; 

    // additional method 
    int capacity() const { 
     return arrayLength; 
    } 
    void reverse(); 

    // iterators to start and end of list 
    class Pointer; 
    Pointer begin() { 
     return Pointer(element); 
    } 
    Pointer end() { 
     return Pointer(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++() // preincrement 
          {++position; return *this;} 
       iterator operator++(int) // postincrement 
          {iterator old = *this; 
          ++position; 
          return old; 
          } 

       // decrement 
       iterator& operator--() // predecrement 
          {--position; return *this;} 
       iterator operator--(int) // postdecrement 
          {iterator old = *this; 
          --position; 
          return old; 
          } 

       // equality testing 
       bool operator!=(const typename arrayList<T>::iterator right) const 
         {return position != right.position;} 
       bool operator==(const typename arrayList<T>::iterator right) const 
         {return position == right.position;} 
      protected: 
       T* position; 
      }; // end of iterator class 


    //  class Pointer: public arrayList<T>::iterator { 

    class Pointer: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 
     Pointer(T *thePosition) { 
      position = thePosition; 
     } 

     Pointer(const Pointer & rhs) 
     { 
      Pointer(rhs.position); 
     } 
     //arithmetic operators 
     Pointer operator+(int n) const; 
     Pointer & operator+=(int n) ; 
     Pointer operator-(int n) const ; 
     Pointer & operator-=(int n) ; 
     reference operator[](difference_type& n)const ; 
     bool operator<(const Pointer &rhs) const ; 
     bool operator<=(const Pointer & rhs) const; 
     bool operator >(const Pointer & rhs) const; 
     bool operator >=(const Pointer &rhs) const ; 
     int operator -(Pointer rhs) ; 

     T operator*() const ; 
     T* operator->() const; 

     // increment 
     Pointer& operator++() ; 
     Pointer operator++(int) ; 

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

     // equality testing 
     bool operator!=(const Pointer right) const; 
     bool operator==(const Pointer right) const; 
    protected: 
     T* position; 
    }; 




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



**// main.cpp** 

int main() { 
    ..... 
    sort(dict.begin(),dict.end(),compare_nocase);/////// error 
     .... 
    return 0; 
} 

的错误是:

c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algobase.h:138:7: instantiated from 'void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = arrayList<std::basic_string<char> >::Pointer, _ForwardIterator2 = arrayList<std::basic_string<char> >::Pointer]' 
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algo.h:111:6: instantiated from 'void std::__move_median_first(_Iterator, _Iterator, _Iterator, _Compare) [with _Iterator = arrayList<std::basic_string<char> >::Pointer, _Compare = bool (*)(std::basic_string<char>, std::basic_string<char>)]' 
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algo.h:2260:7: instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = arrayList<std::basic_string<char> >::Pointer, _Compare = bool (*)(std::basic_string<char>, std::basic_string<char>)]' 
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algo.h:2302:62: instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = arrayList<std::basic_string<char> >::Pointer, _Size = int, _Compare = bool (*)(std::basic_string<char>, std::basic_string<char>)]' 
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algo.h:5250:4: instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = arrayList<std::basic_string<char> >::Pointer, _Compare = bool (*)(std::basic_string<char>, std::basic_string<char>)]' 
..\hw3prob2.cpp:53:45: instantiated from here 
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algobase.h:101:11: error: no matching function for call to 'swap(std::basic_string<char>, std::basic_string<char>)' 
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/move.h:106:5: note: candidates are: void std::swap(_Tp&, _Tp&) [with _Tp = std::basic_string<char>] 
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/basic_string.h:2564:5: note:     void std::swap(std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>] 

我检查了几个小时,但没有想法。感谢您的任何帮助。

Edit

最后,它的工作原理。非常感谢你!!!!!

+0

是否有更多的上下文,该错误信息?如果是这样,我们可以看到它吗? – templatetypedef 2011-02-03 03:07:25

回答

3

arrayList<T>::seamlessPointer不满足随机访问迭代器的要求。一目了然,operator*应返回类型reference,而不是T。它应该有一个默认的构造函数。

编辑此外,此构造函数不会初始化构造的对象。

seamlessPointer(const seamlessPointer & rhs) 
{ 
    seamlessPointer(rhs.position); 
} 

它构建一个临时的匿名seamlessPointer,然后忘掉它。你想要类似

seamlessPointer(const seamlessPointer & rhs) 
    : position(rhs.position) 
{ 
} 
1

我认为你的问题是operator*函数有错误的返回类型。现在它是

T operator*() const 

当它应该是

T& operator*() const 

目前,您返回副本值你遍历,所以当std::sort内部调用std::swap尝试交换值,它将传递数组元素的副本而不是实际的数组元素本身(技术上讲,rvalues代替左值)。将返回类型更改为T&意味着您返回左值,它确实可以交换。

尝试改变这个,看看它是否修复任何东西。