473,224 Members | 1,291 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 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 6939
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: newbiecpp | last post by:
I have hard time to understand run-time environment. Let assume that I have a program that has a simple variable alpha. When this variable is statically allocated, the compiler can use the...
2
by: Brad Pepers | last post by:
For the life of me I can't see why this code won't compile even though its very simple. Any hints? I get errors in NumberValcon::format(int) when it tries to call format() but why doesn't it use...
10
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
26
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37:...
4
by: andrewcw | last post by:
I am moving some code forward from .NET 1.1. I was able to load the XSL file and perform the transform. The MSDN documentation looks like it should be easy. But I get a compile error. Ideas ?...
8
by: sara | last post by:
I have a report that runs fine with data. If there is no data, I have its NO Data event sending a MsgBox and cancelling the report. Then it seems I still get the 2501 message on the Open Report...
4
by: silverburgh.meryl | last post by:
I have code which uses Boost lambda in a template like this: using namespace boost::lambda; template<class T> bool lessThanXY( T& src, T& dest ) { return (src.getY() < dest.getY()); } ...
11
by: Bryan Crouse | last post by:
I am looking a way to do error checking on a string at compile time, and if the string isn't the correct length have then have the compiler throw an error. I am working an embedded software that...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.