473,811 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.b egin(), 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:6 2: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(_Rand omAccessIterato r,
_RandomAccessIt erator) [with _RandomAccessIt erator =
std::_List_cons t_iterator<Rect >]':
.../src/RectUtils.cpp:4 7: 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_in sertion_sort(_R andomAccessIter ator,
_RandomAccessIt erator) [with _RandomAccessIt erator =
std::_List_cons t_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(_Rand omAccessIterato r,
_RandomAccessIt erator) [with _RandomAccessIt erator =
std::_List_cons t_iterator<Rect >]'
.../src/RectUtils.cpp:4 7: 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::__insertio n_sort(_RandomA ccessIterator,
_RandomAccessIt erator) [with _RandomAccessIt erator =
std::_List_cons t_iterator<Rect >]':

Jan 10 '06 #1
7 6968
yi*****@gmail.c om 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.b egin(), 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.c om 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.b egin(), 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:5 4: error: passing 'const std::list<Rect,
std::allocator< Rect> >' as 'this' argument of 'void
std::list<_Tp, _Alloc>::sort(_ StrictWeakOrder ing) [with
_StrictWeakOrde ring = 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.goo glegroups.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.b egin(), 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******** *************@g 47g2000cwa.goog legroups.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.c om 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:5 4: error: passing 'const std::list<Rect,
std::allocator< Rect> >' as 'this' argument of 'void
std::list<_Tp, _Alloc>::sort(_ StrictWeakOrder ing) [with
_StrictWeakOrde ring = 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
3137
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 absolute address of alpha to access to it. What confuses me is that when the variable is dynamically allocated, how does the compiler implement it? We know the address of the variable until run-time. During the compilation, how can we access to the...
2
1419
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 the format() in the parent Valcon class? Also there is an error in the main() code when trying to use MoneyValcon::format(int) but why doesn't main use the one from NumberValcon which is the parent of MoneyValcon? This is on all SuSE 9.2 using...
10
4479
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 Enterplise Linux 3 ES, and applied FixPack fp5_mi00069.tar to it. After creating an instance, starting the database, creating a database, and entering the table definitions, all of which seems to work OK, I entered a tiny 8-row table and can do...
6
4766
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 appreciated. Thanks in advance
26
2893
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: error: using obsolete binding at 'j'
4
9622
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 ? How can I share my XSLT ( there are 2 helper stylesheets, and I check that they are also available... the loaded xsl makes reference to the helper xsl files. Where should I start looking ? // Load the stylesheet. errBefore =...
8
2459
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 command, even though I have the code to trap Err 2501 (from many postings - all looked the same to me) on the button the user pressed to get the report. I never see the code going to my error handling on the button. If I debug, I am in an...
4
2491
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()); } template<class T1, class T2>
11
4278
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 will require individual builds for each device so that the device serial number is contained in the program memory. To do this, the C application must be compiled with the serial number assigned to a variable within the source code file. I...
0
9728
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10648
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10135
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9205
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7670
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.