By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

how to define the third argument to sort()?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
Got it. Thanks a lot.

Sep 29 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.