std::list.sort( compFunc ) error

T

Tim Partridge

I have a std list of student objects and I would like to sort them by
providing a comparison function. I have gotten it to work if my function
is global, but I would like to encapsulate it in a class. For example, my
attempt below fails, but if I move sortFunctor out of the class, it will
compile.

#include <list>
using namespace std;

class foo {
public:
foo(int i) : i(i) {};
~foo() {};
int i;

// compiles if this in the global namespace
bool sortFunctor(const foo &f1, const foo &f2) {
return (f1.i < f2.i);
}
};

int main() {
list< foo > l;
foo f1(8);
foo f2(3);

l.push_back(f1);
l.push_back(f2);

// compiles if foo:: is removed
l.sort(foo::sortFunctor);

return 0;
}

Regards,
Tim Partridge
 
K

Karl Heinz Buchegger

Tim said:
I have a std list of student objects and I would like to sort them by
providing a comparison function. I have gotten it to work if my function
is global, but I would like to encapsulate it in a class. For example, my
attempt below fails, but if I move sortFunctor out of the class, it will
compile.

The trick is, that this class needs an operator()
#include <list>
using namespace std;

class foo {
public:
foo(int i) : i(i) {};
~foo() {};
int i;

// compiles if this in the global namespace
bool sortFunctor(const foo &f1, const foo &f2) {

bool operator() ( const foo& f1, const foo& f2 ) {
return (f1.i < f2.i);
}
};

int main() {
list< foo > l;
foo f1(8);
foo f2(3);

l.push_back(f1);
l.push_back(f2);

// compiles if foo:: is removed
l.sort(foo::sortFunctor);

l.sort( foo() );
 
J

Jeff Flinn

Karl Heinz Buchegger said:
The trick is, that this class needs an operator()
Why?


bool operator() ( const foo& f1, const foo& f2 ) {

What state from (*this) is being used here?

The only thing missing from the OP's class declaration/definition is that
sortFunctor should be static. I would rename sortFunctor to Compare, as it
is not a Functor, and it really just compares to foo's.

Better yet would be to make i private, and provide operator<. That way you
could just call l.sort().

Jeff F
 
K

Karl Heinz Buchegger

Jeff said:

Because the OP wanted to create a Functor. And a functor
is a class, which has such an operator.
What state from (*this) is being used here?

None.
A Functor is an object that can be used in templates when a function
is required.
The only thing missing from the OP's class declaration/definition is that
sortFunctor should be static.

Thats a bad idea in the general case although it works in this
specific case.
 
K

Karl Heinz Buchegger

Dear Jeff. Please disregard my previous post.

After reareading the OP post, I noticed that I completely
read a different program then was written down by the OP.

Somehow I got the expression, that the OP wanted to create
a functor class named foo.
Only after rereading I noticed that foo is the class the OP
builds his list with.


Sorry for the confusion.
 
K

Karl Heinz Buchegger

Karl said:
Dear Jeff. Please disregard my previous post.

After reareading the OP post, I noticed that I completely
read a different program then was written down by the OP.

Somehow I got the expression, that the OP wanted to create

typed to quick: ... got the impression, ...
 
J

Jeff Flinn

Karl Heinz Buchegger said:
Dear Jeff. Please disregard my previous post.

After reareading the OP post, I noticed that I completely
read a different program then was written down by the OP.

Somehow I got the expression, that the OP wanted to create
a functor class named foo.
Only after rereading I noticed that foo is the class the OP
builds his list with.


Keine Probleme.

The OP's choice of name "sortFunctor" didn't help.

Jeff F
 

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

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top