Grade:
Class: private: ... vector<string> words; vector< list<int> > vints; public: myFunction(...)
I call sorting on a non-empty list in another member function:
void myClass::myFunction (...) { ... if (!vints[i].empty()) vints[i].sort(sortFunc); ... }
My sort function:
bool myClass::sortFunc(const int& i, const int& j) { return (words[i] < words[j]); }
Error:
error: no matching function for call to 'std::list<int, std::allocator<int> >::sort(<unresolved overloaded function type>)' /usr/include/c++/4.4/bits/list.tcc:301: note: candidates are: void std::list<_Tp, _Alloc>::sort() [with _Tp = int, _Alloc = std::allocator<int>] /usr/include/c++/4.4/bits/list.tcc:378: note: void std::list<_Tp, _ Alloc>::sort(_StrictWeakOrdering) [with _StrictWeakOrdering = bool (SuperWordSearch::*) (const int&, const int&), _Tp = int, _Alloc = std::allocator<int>]
I researched and came across the following questions:
C ++ Custom comparison function for list :: sort
The problem of sorting a list of pointers
Error in std :: list :: sort with custom comparator (expected primary expression before ')' token
and they would be enough if it weren’t for the fact that in this class sortFunc depends on the WORDS member variable for this instance of the object. Therefore, I cannot make the comparison function (sortFunc) static or global
EDIT: just came across this How to sort a std list: when do you need member data? and he offers a solution by creating a friends class, but can this be done inside the user class itself?
source share