简体   繁体   中英

I'm trying to create an iterator for my DynamicArray class. Why doesn't STL sort work with my iterator?

I've created a DynamicArray class and implemented an iterator for it. One of my .cpp files takes a DynamicArray of n length filled with random values, and is supposed to sort it with STL sort. However, I've tinkered for a few hours and have always gotten this error whenever trying to run sort with begin() and end() as the parameters:

no type named 'value_type' in 'struct std::iterator_traits<DynamicArray<double>::iterator>'

I'll give my DynamicArray prototypes for reference:

template<class T>
class DynamicArray {
public:
  static T dummy;
  class iterator { // iterator for begin() and end() functions
  private:
    T* ptr;
  public:
    iterator() { ptr = NULL; } // constructor, sets pointer to NULL
    iterator( T* p ) { ptr = p; } // parameterized constructor, sets pointer to data in DynamicArray
    const T& operator*() const { return *ptr; } // pointer operator, returns pointer
    void operator++() { if( ptr ) ptr++; } // pre-increment operator, increments pointer
    void operator++( int ) { if( ptr ) ptr++; } // post-increment operator, increments pointer
    bool operator!=( const iterator & other ) { return ptr != other.ptr; } // does not equal operator
  };
private:
  T* data;
  bool* inUse;
  unsigned int size;
  unsigned int capacity;
public:
  DynamicArray();
  DynamicArray( const DynamicArray<T> & );
  virtual ~DynamicArray();
  DynamicArray<T> & operator=( const DynamicArray<T> & );
  T operator[]( unsigned int index ) const;
  T& operator[]( unsigned int index );
  unsigned int getSize() const;
  unsigned int getCapacity() const;
  bool containsKey( unsigned int index ) const;
  void deleteKey( unsigned int index );
  iterator begin() const { return iterator( data ); }
  iterator end() const { return iterator( data + size ); }
  vector<unsigned int> keys() const;
  void clear();
private:
  void copy( const DynamicArray<T> & );
  void deleteIt();
  void setCapacity( unsigned int newCap = 10 );
};

What does this error mean? How can I fix it? Thanks in advance.

Your iterator does not meet the C++ library requirements for an iterator. It's not as easy as simply naming something an iterator , in order to create an iterator. The requirements for an iterator span over 30 pages, in the C++ standard.

In nearly all cases, the easiest way to implement a custom iterator is to inherit from the std::iterator template. The template takes two required parameters, and three optional parameters, in order to synthesize all the required attributes of the custom iterator.

Briefly looking over your iterator's code, it looks to me like you can almost support the requirements of forward iterators, so try inheriting your iterator class from std::iterator<std::forward_iterator_tag,T> .

The reason I say "almost" is because your operator++ operators are broken. Their return value is wrong, and the post-increment version is also wrong. This may preclude your iterator from working correctly, with some algorithms in the C++ library, until you fix this. But your biggest problem is lack of the implemenation of the required iterator traits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM