473,320 Members | 2,107 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,320 software developers and data experts.

how to define the third argument to sort()?

Hello, I met some problems when trying to sort a list that has
shared_ptr in it. here is the non-compliable code.

test.cpp:
/********************* begin of code *************************/
#include <boost/shared_ptr.hpp>
#include <list>
class T{ // simplified this class to the simplest form
public:
T(int t):capacity(t){}
int capacity;
};

// I want to use this function as the third argument for sort() in
main.
bool capacityCompare(boost::shared_ptr<T&lhs, boost::shared_ptr<T>
&rhs) // line 9
{
return (lhs->capacity < rhs->capacity);
}

int main()
{
boost::shared_ptr<TT1 (new T(2));
boost::shared_ptr<TT2 (new T(3));
list<boost::shared_ptr<T TList; // a list that consists of ptrs
TList.push_back(T1);
TList.push_back(T2);
sort(TList.begin(), TList.end(), capacityCompare()); // line 21
return 1;
}
/*************** end of code ************/

/****** the first compilig error **********/
test.cpp: In function `int main()':
test.cpp:9: error: too few arguments to function 'bool
capacityCompare(boost::shared_ptr<T>&, boost::shared_ptr<T>&)'
test.cpp:21: error: at this point in file
************************************************** ********
Does anybody see what's wrong here? My guess is that the type of the
argument in capacityCompare() is wrong. If I am correct, how should I
define such a pred function?

BTW, compiler also has a warning:
/usr/include/gcc/darwin/4.0/c++/backward/backward_warning.h:32:2:
warning: #warning This file includes at least one deprecated or
antiquated header. Please consider using one of the 32 headers found in
section 17.4.1.2 of the C++ standard. Examples include substituting the
<Xheader for the <X.hheader for C++ includes, or <iostreaminstead
of the deprecated header <iostream.h>. To disable this warning use
-Wno-deprecated.

How can I get rid of it?

Thanks a lot.

P.S. The command that I use is:
g++ -Wall -g -I/usr/local/lib/boost_1_33_1 test.cpp

Sep 29 '06 #1
3 2758
Sorry, the second line should be:
#include <list.h>
I was trying to get rid of the warning I mentioned below. But of course
it didn't work.

zh************@gmail.com wrote:
Hello, I met some problems when trying to sort a list that has
shared_ptr in it. here is the non-compliable code.

test.cpp:
/********************* begin of code *************************/
#include <boost/shared_ptr.hpp>
#include <list>
class T{ // simplified this class to the simplest form
public:
T(int t):capacity(t){}
int capacity;
};

// I want to use this function as the third argument for sort() in
main.
bool capacityCompare(boost::shared_ptr<T&lhs, boost::shared_ptr<T>
&rhs) // line 9
{
return (lhs->capacity < rhs->capacity);
}

int main()
{
boost::shared_ptr<TT1 (new T(2));
boost::shared_ptr<TT2 (new T(3));
list<boost::shared_ptr<T TList; // a list that consists of ptrs
TList.push_back(T1);
TList.push_back(T2);
sort(TList.begin(), TList.end(), capacityCompare()); // line 21
return 1;
}
/*************** end of code ************/

/****** the first compilig error **********/
test.cpp: In function `int main()':
test.cpp:9: error: too few arguments to function 'bool
capacityCompare(boost::shared_ptr<T>&, boost::shared_ptr<T>&)'
test.cpp:21: error: at this point in file
************************************************** ********
Does anybody see what's wrong here? My guess is that the type of the
argument in capacityCompare() is wrong. If I am correct, how should I
define such a pred function?

BTW, compiler also has a warning:
/usr/include/gcc/darwin/4.0/c++/backward/backward_warning.h:32:2:
warning: #warning This file includes at least one deprecated or
antiquated header. Please consider using one of the 32 headers found in
section 17.4.1.2 of the C++ standard. Examples include substituting the
<Xheader for the <X.hheader for C++ includes, or <iostreaminstead
of the deprecated header <iostream.h>. To disable this warning use
-Wno-deprecated.

How can I get rid of it?

Thanks a lot.

P.S. The command that I use is:
g++ -Wall -g -I/usr/local/lib/boost_1_33_1 test.cpp
Sep 29 '06 #2
zh************@gmail.com wrote:
Hello, I met some problems when trying to sort a list that has
shared_ptr in it.
std::list doesn't provide random access iterators, so you can't sort it
with std::sort. Instead, use list::sort.

The problem you're seeing is because the form of the third argument is
wrong. capacityCompare is a function, so capacityCompare() is a function
call and, indeed, it doesn't have the right number of arguments. Get rid
of the parentheses, so you're passing the address of the function.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 29 '06 #3
Got it. Thanks a lot.

Sep 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: velthuijsen | last post by:
The STL sort only accepts a function that is defined in the form of bool Fname(<type>, <type>) in which <type> is the type of range to be sorted. I'm looking for a way to be able to sort the range...
3
by: theotyflos | last post by:
Hi all, I have the following: /*--- SNIP ---*/ typedef struct Argument_s { char *address; int type;
9
by: pozz | last post by:
Hi all, I have the below #defines #define NUMBER1 30 #define NUMBER2 50 #define SUM (NUMBER1+NUMBER2) #define STRING1 "Byte: \x30" #define STRING2 "Byte: \x50"...
57
by: Mike Malone | last post by:
A colleague of mine is proposing that we use a set of preprocessor definitions to make our C code more readable: #define BEGIN { #define ENG } #define EQ == etc. My initial...
6
by: thesushant | last post by:
hi, whats the use of third argument to main( ), i.e. environmental parameters.... if i am not wrong ? 1st 1 is argc 2nd is argv and what bout the 3rd 1??????????? sushant
29
by: Ancient_Hacker | last post by:
It sure would be nice if I could have a macro that add a level of indirection to its argument. So if I write: AddIndirection( X ) The macro AddIndirection will do: #define X (*X) ...
69
by: RC | last post by:
I know how to do this in JavaScript by window.open("newFile.html", "newTarget", "scrollbars=no,resizable=0,width=200,height=200"); The browser will open a new window size 200x200, not allow...
10
by: Yevgen Muntyan | last post by:
Consider the following macro: #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) The intent is to wrap raw memory allocation of N bytes into a macro which returns allocated chunk of memory...
1
by: Giovanni Toffoli | last post by:
Hi, I'm not in the mailing list. By Googling, I stepped into this an old post: (Thu Feb 14 20:40:08 CET 2002) of Jeff Shannon:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.