I have a class which basically looks like this(i removed unnecessary code):
class WilxList {
private:
struct Test{
double number;
int sign;
int rank;
};
bool testSorter(const Test & x1, const Test & x2);
public:
WilxList(std::vector<double> &argNumbers, std::string argName, int numberOfTests);
};
I am trying to sort a vector of Test structs like that:
WilxList::WilxList(std::vector<double> &argNumbers, std::string argName, int numberOfTests)
{
//Omitted code
std::vector<Test> sortedTests;
//Omitted code where Tests are created and added to the vector inside for loop
std::sort(sortedTests.begin(), sortedTests.end(), testSorter); //ERROR
}
The error i get is:
error C3867: 'WilxList::testSorter': function call missing argument list;
use '&WilxList::testSorter' to create a pointer to member
c:\users\stenver\documents\visual studio 2012\projects\wilxoniastakutest\wilxoniastakutest\wilxlist.cpp
testSorteris a non static member function of the classWilxListwhich is causing the difficulty. To use function pointers to non static member functions is relatively more difficult, and is impossible to use in cases which expect a free (or static member) function like thesortalgorithm.One option to fix this is to move
testSorterinto the struct itself and rename it tooperator<. This will let you do stuff likex1<x2and you can call sort as juststd::sort(sortedTests.begin(), sortedTests.end());This is assuming the function actually represents a<operation, if it isnt, it might get confusing and would be neater to just make it a static member function. The errors will go away in either case.