473,378 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

std::list.sort( compFunc ) error

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
Jul 22 '05 #1
6 3126
Tim Partridge wrote:

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() );

return 0;
}


--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
Tim Partridge wrote:

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()


Why?

#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);
}
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
};

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() );

return 0;
}


--
Karl Heinz Buchegger
kb******@gascad.at

Jul 22 '05 #3
Jeff Flinn wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
Tim Partridge wrote:

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()


Why?


Because the OP wanted to create a Functor. And a functor
is a class, which has such 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);
}


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
Jeff Flinn wrote:


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5
Karl Heinz Buchegger wrote:

Jeff Flinn wrote:


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, ...
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
Jeff Flinn wrote:


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
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
1
by: Ravi | last post by:
I came across code which contains: std::sort<unsigned char*>((newAnswer.begin()+1),newAnswer.end()); with newAnswer defined as std::vector<unsigned char>& newAnswer However upon...
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
7
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
11
by: velthuijsen | last post by:
I tried taking a list and pass it through std::sort like the following: sort(Unsorted.begin(), Unsorted.end()); I got an error back stating that the list iterator doesn't have a binary...
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
5
by: matthias_k | last post by:
Hi, I need to sort elements of a std::list using a function predicate, something like: bool predicate( const& M m1, const& M m2 ) { return m1.somedata < m2.somedata; } I tried to call...
3
by: Ray D. | last post by:
Hey all, I'm trying to pass a list into a function to edit it but when I compile using g++ I continue to get the following error: maintainNeighbors.cpp:104: error: invalid initialization of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.