Connecting Tech Pros Worldwide Forums | Help | Site Map

how to define the third argument to sort()?

zhang.yongpeng@gmail.com
Guest
 
Posts: n/a
#1: Sep 29 '06
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


zhang.yongpeng@gmail.com
Guest
 
Posts: n/a
#2: Sep 29 '06

re: how to define the third argument to sort()?


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.

zhang.yongpeng@gmail.com wrote:
Quote:
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
Pete Becker
Guest
 
Posts: n/a
#3: Sep 29 '06

re: how to define the third argument to sort()?


zhang.yongpeng@gmail.com wrote:
Quote:
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.
zhang.yongpeng@gmail.com
Guest
 
Posts: n/a
#4: Sep 29 '06

re: how to define the third argument to sort()?


Got it. Thanks a lot.

Closed Thread