List.Sort(cmp_fnc) term does not evaluate to a function taking 2 arguments?

I am trying to customize my sorting of a LIST using a compare_function, but for some odd reason it keeps giving me the following error:
error C2064: term does not evaluate to a function taking 2 arguments

Specifically, I have list created within class B which needs to be sorted, the list contains elements of type A* as shown below:

Class B code:
[Code]
B::B()
{
list<A*> AList;

AList.push_back(new A(ID_1, CODE_1));
AList.push_back(new A(ID_2, CODE_2));

AtList.sort(&B::compareID);
}

bool B::compareID(A* first, A* second)
{
return true; // test for now
}
[Code]

Just for completness the following is the code for Class A:
[Code]
Class A
{
long ID;
string sCode;
A(_ID, _sCode) : ID(_ID), sCode(_sCode) {};
}
[Code]

So, pretty much I just want to sort AList by ID, but for some odd reason this generates:
error C2064: term does not evaluate to a function taking 2 arguments

Also, in the future I am going to want to create a compareCode() function to also compare by code ... thought if one way works so will the other ...

Any clues, hints, or help would be greatly appreciated.
Thanks,
Last edited on
Make compareID() a regular function instead of a method.
This is what mem_fun is for as well.
http://www.cplusplus.com/reference/std/functional/mem_fun/

Also, I think that boost::bind does something similar. If you are able to use boost, I'd recommend looking into that.

I'm not sure what the best answer is from a design standpoint to be honest. There are quite a few ways to solve your problem. Helios' suggestion is also fine. By regular function I think he means global (define it outside of class). Another possibility is to make it a static function of the class. Another possibility would be to declare a functor class and pass a functor to list::sort. Personally, I think it is better if the function is a member of A since A objects are being sorted. That just makes more sense to me.

Keep in mind that for sorting the predicate, whichever way you do it, must be a pure function. Google "pure function" if you don't know what that means.
Last edited on
If you want to get fancy, you could make it a static method of B. Static members are just globals in a different namespace, so it's basically the same. Personally, I would go for the simpler global function.
Topic archived. No new replies allowed.