When I sort a list with object's pointer, it will be error

When I sort a list with pointer object it will be error.

#include <iostream>

#include <string>

#include <list>

#include <vector>

#include <algorithm>

#include <functional>

using namespace std;

/* class Person

*/

class Person {

private:

string fn;// first name

string ln;// last name

public:

Person() {

}

Person(const string& f, const string& n)

: fn(f), ln(n) {

}

string firstname() const;

string lastname() const;

// ...

};

inline string Person::firstname() const {

return fn;

}

inline string Person::lastname() const {

return ln;

}

/* binary function predicate:

* - returns whether a person is less than another person

*/

bool personSortCriterionFunction (const Person* p1, const Person* p2)

{

return p1->lastname()>p2->lastname() ||

(!(p2->lastname()>p1->lastname()) &&

p1->firstname()>p2->firstname());

}

int main()

{

// create some persons

Person p1("nicolai","josuttis");

Person p2("ulli","josuttis");

Person p3("anica","josuttis");

Person p4("lucas","josuttis");

Person p5("lucas","otto");

Person p6("lucas","arm");

Person p7("anica","holle");

// insert person into collection coll

list<Person*> coll;

coll.push_back(&p1);

coll.push_back(&p2);

coll.push_back(&p3);

coll.push_back(&p4);

coll.push_back(&p5);

coll.push_back(&p6);

coll.push_back(&p7);

// Sort list

coll.sort(personSortCriterionFunction);

return 0;

}

# CC listpointer.cxx

"listpointer.cxx", line 69: Error: Could not find a match for std::list<Person*>::sort(bool(const Person*,const Person*)) needed in main().

1 Error(s) detected.

It is correct when I change to list<Person> coll and other corresponding lines.

CC listpointer.cxx

Who know why the reason?

[2112 byte] By [w20088a] at [2007-11-27 11:53:52]
# 1

Inclusion of "-library=stlport4" on the command line solves the problem. Refer to CC(1) for more info on this option.

mkartasheva at 2007-7-29 18:52:42 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 2

This has been discussed earlier on the forum and I don't think you'll like the answere.

clamage45

"You have run into a documented limitation of the default libCstd implementation of the C++ standard library. The default library is not a complete implementation, and In particular, the template "sort" member of list that takes a parameter is not available.

We still use this library for binary compatibility with earlier releases.

The optional STLport version of the library is a full implementation of hte C++ Standard Library. If you don't need binary compatibility, meaning you don't need to link to code that uses libCstd, you can add the option -library=stlport4 to every CC command line, compiling and linking. "

Lars_Va at 2007-7-29 18:52:42 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 3

For a list of pointers you could try to change definition of personSortCriterionFunction to:

bool personSortCriterionFunction (Person * const &p1, Person * const &p2)

{

...

}

dmikha at 2007-7-29 18:52:42 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 4

Replace:

[code]bool personSortCriterionFunction (const Person* p1, const Person* p2)[/code]

by:

[code]bool personSortCriterionFunction (const Person* const& p1, const Person* const& p2)[/code]

and:

[code]list < Person* >[/code]

by:

[code]list < const Person* >[/code]

I don't know exactly what you did with [code]list < Person >[/code] that worked, but I guess it involved references in the prototype of the function.

Marc_Glissea at 2007-7-29 18:52:42 > top of Java-index,Development Tools,Solaris and Linux Development Tools...