473,748 Members | 9,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(con st 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::sor tFunctor);

return 0;
}

Regards,
Tim Partridge
Jul 22 '05 #1
6 3140
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(con st 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::sor tFunctor);
l.sort( foo() );

return 0;
}


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

"Karl Heinz Buchegger" <kb******@gasca d.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(con st 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::sor tFunctor);


l.sort( foo() );

return 0;
}


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

Jul 22 '05 #3
Jeff Flinn wrote:

"Karl Heinz Buchegger" <kb******@gasca d.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(con st 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******@gasca d.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 "sortFuncto r" 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
3502
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< ValuePair_t, ValuePair_t, bool> ValuePair_IsLess; void SortAscend(const ValuePair_IsLess& isLess_) {
1
2364
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 attempting to compile the above code I got the following errors (I am using g++ ver 3.3.1 on Cygwin)
4
3349
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 if so, why not? Shouldn the compiler be able to tell which of he overloaded functions to use? The second A::comp() is the one I accidently added and gives the error message (in Borland C++Builder 6) Unit1.cpp E2285 Could not find a match for
7
3545
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 there is a huge overhead of using this function, which comes from copying the function object, i.e., the object that compares the elements of the vector, which is passed to the function not by reference but by value. Now, at the end of this mail...
11
5789
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 substraction operator. Peeking in the algoritm header file it's clear why seeing that sort there calls _sort(_First, _Last, _Last-_First) which might be a tad challenging seeing that the different values stored in a list do not need to be stored...
5
1786
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
7358
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 std::list::sort with such a predicate but that doesn't work:
3
2527
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 non-const reference of type 'std::list<HostID, std::allocator<HostID&' from a temporary of type 'std::list<HostID, std::allocator<HostID*' helpers.cpp:99: error: in passing argument 1 of `void
0
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.