471,599 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

compile error trying to call STL sort

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>]':

Jan 10 '06 #1
7 6845
yi*****@gmail.com wrote:
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
Jan 10 '06 #2
yi*****@gmail.com wrote:
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
Jan 10 '06 #3
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.

Jan 10 '06 #4

<yi*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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?

Jan 10 '06 #5

<yi*****@gmail.com> skrev i meddelandet
news:11*********************@g47g2000cwa.googlegro ups.com...
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
Jan 10 '06 #6
yi*****@gmail.com wrote:
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.

Jan 10 '06 #7
Thanks for all the help.

Jan 10 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by newbiecpp | last post: by
2 posts views Thread by Brad Pepers | last post: by
6 posts views Thread by Peter Frost | last post: by
26 posts views Thread by Martin Jørgensen | last post: by
4 posts views Thread by andrewcw | last post: by
4 posts views Thread by silverburgh.meryl | last post: by
11 posts views Thread by Bryan Crouse | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.