how to define the third argument to sort()?

Z

zhang.yongpeng

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<T> T1 (new T(2));
boost::shared_ptr<T> T2 (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
<X> header for the <X.h> header for C++ includes, or <iostream> instead
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
 
Z

zhang.yongpeng

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.
 
P

Pete Becker

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top