I wrote myself a compare function for sort().
It worked well when I put it this way.
bool comp(string a, string b)
{
...;
}
int main()
{
sort(...,...,comp);
}
However, when I put all this inside a class, say:
class Test {
public:
bool comp(string a,string b)
{
...;
}
vector <string> CustomSort(vector <string> str) {
sort(...,...,comp);
}
};
There is a compile error “No matching function for call to ‘sort ……’.
Why would this happen?
Any nonstatic member function of class
Xhas an extra argument – a reference/pointer to (const)Xwhich becomesthis. Therefore a member function’s signature isn’t such that can be digested bysort. You need to useboost::bindorstd::mem_funorstd::mem_fun_ref. When using C++11, you can usestd::bind.Come to think of it, the best solution in this case would be to make your comp function static, because it doesn’t need
thisat all. In this case your original code will work without change.