By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Sorting STL list by custom comperator

P: 9
Hey guys, i looked at some sorting examples, and i thought i jsut had to build a comperator which returns bool, in order to keep my list sorted by that value.

here's the relevent code:

Expand|Select|Wrap|Line Numbers
  1. bool CityHall::EmployeeComp (Employee *e1,Employee *e2) const
  2. {
  3. if (e1->GetID()<e2->GetID())
  4.     return true;
  5. return false;
  6. }
  7.  
  8. char** CityHall::PrintAllWorkersByLastName() const
  9. {
  10.     if (m_cityHallWorkers->size()==0)
  11.          return NULL;
  12.      m_cityHallWorkers->sort(EmployeeComp);
  13. .
  14. .
  15. .


and i get:

Error 1 error C3867: 'CityHall::EmployeeComp': function call missing argument list; use '&CityHall::EmployeeComp' to create a pointer to member.
Error 2 error C2660: 'std::list<_Ty>::sort' : function does not take 1 arguments.

I know the comperator takes to args, but in all the examples they didn't send any, what should i send it if i should?!

Thank you very much for ur help and time
Sep 3 '10 #1
Share this Question
Share on Google+
13 Replies


100+
P: 207
Here is a page on the sort function for a list. The sort method can take a custom comparitor for sorting the list, but why would you? You sort your list by the < operator just like the default sorting method does. Why don't you just define the < operator for your Employee class?

http://www.cplusplus.com/reference/stl/list/sort/
Sep 3 '10 #2

100+
P: 687
I know the comperator takes to args.

I don't get it. What does comparator takes to them? Anyway, the error message is quite clear - "use '&CityHall::EmployeeComp' to create a pointer to member." -
will
Expand|Select|Wrap|Line Numbers
  1. m_cityHallWorkers->sort(&CityHall::EmployeeComp);
work?
Sep 3 '10 #3

Banfa
Expert Mod 5K+
P: 8,916
Your problem is that you have tried to call a member function of CityHall. However to call a member function you need to have an instance of the class to call it on and there is no way to pass that instance into list::sort.

There is a solution:

Remove EmployeeComps dependency on having an object of type CityHall. This is easy make it a static member function which do not require an object to all and pass it to list::sort through the class CityHall::EmployeeComp . An alternative is to move the method out of the CityHall class entirely but then that puts it into global scope which is probably not good.
Sep 3 '10 #4

P: 9
hype261 --> I will try defining < for my class, i was just wondering why doesn't it work, and what should be the syntax for it to work

newb16-->
when u do that u get:


Error 2 error C2064: term does not evaluate to a function taking 2 arguments

and it jumps a file called "xutility" where it show the error in it
Sep 3 '10 #5

P: 9
@ Banfa --> how do i turn it into static, should i just add
"static" before the func decleration at cityhall.h?
Sep 3 '10 #6

Banfa
Expert Mod 5K+
P: 8,916
Yes .
Sep 3 '10 #7

P: 9
and then i get:


Error 1 error C2272: 'EmployeeComp' : modifiers not allowed on static member functions

same code as before, just added static at the h file
Sep 3 '10 #8

Banfa
Expert Mod 5K+
P: 8,916
Like I said you will need to access it through the class so

m_cityHallWorkers->sort(CityHall::EmployeeComp);
Sep 3 '10 #9

P: 9
this is how it is written in the h file:
static bool EmployeeComp (Employee *e1,Employee* e2) const;

still gives the same error when i did:

m_cityHallWorkers->sort(CityHall::EmployeeComp);

Error 1 error C2272: 'EmployeeComp' : modifiers not allowed on static member functions
Sep 3 '10 #10

100+
P: 687
remove const modifier.
Sep 3 '10 #11

P: 9
well if only i knew where do i even modify a const here..

anyway i think i'll just go with definind < operator

thank you all!!

if someone can still point what the problem is i'll be happy to hear!:)
Sep 3 '10 #12

100+
P: 207
Remove the const at the end of this statement.

Expand|Select|Wrap|Line Numbers
  1. static bool EmployeeComp (Employee *e1,Employee* e2) const;
Sep 3 '10 #13

P: 9
thnx alot, this works!
Sep 3 '10 #14

Post your reply

Sign in to post your reply or Sign up for a free account.