Reputation: 4525
In the arrayList I customize the iterator and then use sort of stl and the iterator to sort string (words).
**//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;
}
The errors are :
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>]
I check for several hours but have no ideas. Thank you for any help.
Edit
Finally, it works. Thank you very much!!!!!
Upvotes: 1
Views: 823
Reputation: 72431
arrayList<T>::seamlessPointer
does not satisfy the requirements of a random access iterator. At a glance, operator*
should return the type reference
, not T
. And it should have a default constructor.
Edit: Also, this constructor doesn't initialize the constructed object.
seamlessPointer(const seamlessPointer & rhs)
{
seamlessPointer(rhs.position);
}
It constructs a temporary anonymous seamlessPointer
and then forgets about it. You want something like
seamlessPointer(const seamlessPointer & rhs)
: position(rhs.position)
{
}
Upvotes: 3
Reputation: 373002
I think that your problem is that the operator*
function has the wrong return type. Right now it's
T operator*() const
When it should be
T& operator*() const
Currently, you return a copy of the values you're iterating over, so when std::sort
internally calls std::swap
to try to exchange the values, it will be passing in copies of the array elements rather than the actual array elements themselves (more technically, rvalues instead of lvalues). Changing the return type to T&
means that you return the lvalue, which can indeed be swapped.
Try changing this and see if it fixes anything.
Upvotes: 1