compile error trying to call STL sort

Y

yinglcs

Hi,

I have a function which calls stl sort(). I pass in a STL list of
'Rect' (my own class), like this:

void sortListY(const list<Rect>& rectList) {

sort(rectList.begin(), rectList.end()); // if I comment out this
line, the program compiles fine.

}

When I try to compile it, i have these error, can you please tell me
why?


++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc/RectUtils.o
.../src/RectUtils.cpp
.../src/RectUtils.cpp:62:2: warning: no newline at end of file
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/bits/stl_algo.h:
In function 'void std::sort(_RandomAccessIterator,
_RandomAccessIterator) [with _RandomAccessIterator =
std::_List_const_iterator<Rect>]':
.../src/RectUtils.cpp:47: instantiated from here
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/bits/stl_algo.h:2569:
error: no match for 'operator-' in '__last - __first'
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/bits/stl_algo.h:
In function 'void std::__final_insertion_sort(_RandomAccessIterator,
_RandomAccessIterator) [with _RandomAccessIterator =
std::_List_const_iterator<Rect>]':
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/bits/stl_algo.h:2570:
instantiated from 'void std::sort(_RandomAccessIterator,
_RandomAccessIterator) [with _RandomAccessIterator =
std::_List_const_iterator<Rect>]'
.../src/RectUtils.cpp:47: instantiated from here
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/bits/stl_algo.h:2213:
error: no match for 'operator-' in '__last - __first'
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/bits/stl_algo.h:2215:
error: no match for 'operator+' in '__first + 16'
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/bits/stl_algo.h:2216:
error: no match for 'operator+' in '__first + 16'
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/bits/stl_algo.h:
In function 'void std::__insertion_sort(_RandomAccessIterator,
_RandomAccessIterator) [with _RandomAccessIterator =
std::_List_const_iterator<Rect>]':
 
V

Victor Bazarov

Hi,

I have a function which calls stl sort(). I pass in a STL list of
'Rect' (my own class), like this:

void sortListY(const list<Rect>& rectList) {

sort(rectList.begin(), rectList.end()); // if I comment out this
line, the program compiles fine.

}

When I try to compile it, i have these error, can you please tell me
why?

Because iterators of 'std::list' are not random-access iterators. The
template 'std::list' has it own member 'sort' function. Use it.

V
 
D

Duane Hebert

Hi,

I have a function which calls stl sort(). I pass in a STL list of
'Rect' (my own class), like this:

void sortListY(const list<Rect>& rectList) {

sort(rectList.begin(), rectList.end()); // if I comment out this
line, the program compiles fine.

}

What happens if you drop the const?
 
J

Jeff Flinn

Hi,

I have a function which calls stl sort(). I pass in a STL list of
'Rect' (my own class), like this:

void sortListY(const list<Rect>& rectList) {

sort(rectList.begin(), rectList.end()); // if I comment out this
line, the program compiles fine.

sort requires random access iterators, list provides bi-directional
iterators. Hence, list provides a sort member funtion.

Use either:

rectlist.sort(); // req's Rect to be less than comparable

or

rectlist.sort( somepredicate );

Jeff Flinn
 
Y

yinglcs

Thanks for all the help.

But I need to pass in a comparator, like this:

bool compare_rect_y(const Rect &r1, const Rect &r2)
{
return r1.y < r2.y;
}

void sortListY(const list<Rect>& rectList) {

rectList.sort( compare_rect_y);

}

I still can't get it to compile, I have this error:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc/RectUtils.o
.../src/RectUtils.cpp
.../src/RectUtils.cpp: In function 'void sortListY(const
std::list<Rect, std::allocator<Rect> >&)':
.../src/RectUtils.cpp:54: error: passing 'const std::list<Rect,
std::allocator<Rect> >' as 'this' argument of 'void
std::list<_Tp, _Alloc>::sort(_StrictWeakOrdering) [with
_StrictWeakOrdering = bool (*)(const Rect&, const Rect&), _Tp = Rect,
_Alloc = std::allocator<Rect>]' discards qualifiers
make: *** [src/RectUtils.o] Error 1
make: Target `all' not remade because of errors.

Thanks for further assistance.
 
J

Jeff Flinn

Thanks for all the help.

But I need to pass in a comparator, like this:

bool compare_rect_y(const Rect &r1, const Rect &r2)
{
return r1.y < r2.y;
}

void sortListY(const list<Rect>& rectList) {

rectList.sort( compare_rect_y);

You're attempting to modify a const list, as the error message states.

Jeff
}

I still can't get it to compile, I have this error:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc/RectUtils.o
../src/RectUtils.cpp
../src/RectUtils.cpp: In function 'void sortListY(const
std::list<Rect, std::allocator<Rect> >&)':
../src/RectUtils.cpp:54: error: passing 'const std::list<Rect,
std::allocator<Rect> >' as 'this' argument of 'void
std::list<_Tp, _Alloc>::sort(_StrictWeakOrdering) [with
_StrictWeakOrdering = bool (*)(const Rect&, const Rect&), _Tp = Rect,
_Alloc = std::allocator<Rect>]' discards qualifiers
make: *** [src/RectUtils.o] Error 1
make: Target `all' not remade because of errors.

Thanks for further assistance.
 
B

Bo Persson

Thanks for all the help.

But I need to pass in a comparator, like this:

bool compare_rect_y(const Rect &r1, const Rect &r2)
{
return r1.y < r2.y;
}

void sortListY(const list<Rect>& rectList) {

rectList.sort( compare_rect_y);

}

I still can't get it to compile,

Yes, the const is another problem. Sorting the list will update it.
..-)


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top