473,782 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_p tr<T>
&rhs) // line 9
{
return (lhs->capacity < rhs->capacity);
}

int main()
{
boost::shared_p tr<TT1 (new T(2));
boost::shared_p tr<TT2 (new T(3));
list<boost::sha red_ptr<T TList; // a list that consists of ptrs
TList.push_back (T1);
TList.push_back (T2);
sort(TList.begi n(), 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_p tr<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_warnin g.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 <iostreaminstea d
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 2785
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_p tr<T>
&rhs) // line 9
{
return (lhs->capacity < rhs->capacity);
}

int main()
{
boost::shared_p tr<TT1 (new T(2));
boost::shared_p tr<TT2 (new T(3));
list<boost::sha red_ptr<T TList; // a list that consists of ptrs
TList.push_back (T1);
TList.push_back (T2);
sort(TList.begi n(), 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_p tr<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_warnin g.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 <iostreaminstea d
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
1444
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 on a different set of types then the range. I currently have worked out a way to do it (see code below) but it seems fairly involved and I was wondering if there is a better way of doing it?
3
10584
by: theotyflos | last post by:
Hi all, I have the following: /*--- SNIP ---*/ typedef struct Argument_s { char *address; int type;
9
3095
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" If I change NUMBER1 and NUMBER2 I must change STRING1 and STRING2 consequently.
57
7523
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 reaction is "Yuck!" (Not too different from the FAQ, which just says "Bleah").
6
3117
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
2333
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) so after that point whenever you use X it gets replaced by (*X)
69
13490
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 resize and no auto horizontal, vertical scrolling bars. I am wonder can I do the similar inside a HTML file like
10
2607
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 to be used as a Type structure. The background: I was beaten many times (it surely is not my exclusive experience) by functions which return and accept void*. There are such functions,
1
2876
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: http://mail.python.org/pipermail/python-list/2002-February/128438.html <<< def SortOnItem(mylist, index): templist = , line) for line in mylist ]
0
10311
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
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
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
5378
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...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
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.