How can I beat this C ++ Vector Sorting error?

Here is the problem code I'm trying to compile:

bool TeamMatcher::simpleComparator(Student first, Student second){
  return (first.numberOfHrsAvailable < second.numberOfHrsAvailable);
}

void TeamMatcher::sortRosters(){
  sort(rosterExcellent.begin(), rosterExcellent.end(), simpleComparator);
  sort(rosterGood.begin(), rosterGood.end(), simpleComparator);
  sort(rosterOK.begin(), rosterOK.end(), simpleComparator);
  sort(rosterPoor.begin(), rosterPoor.end(), simpleComparator);
  sort(rosterNoSay.begin(), rosterNoSay.end(), simpleComparator);
}

Then here is the error I get:

TeamMatcher.C: In member function ‘void TeamMatcher::sortRosters()’:
TeamMatcher.C:51: error: no matching function for call tosort(__gnu_cxx::__normal_iterator<Student*, std::vector<Student, std::allocator<Student> > >, __gnu_cxx::__normal_iterator<Student*, std::vector<Student, std::allocator<Student> > >, <unresolved overloaded function type>)’
/usr/include/c++/4.2.1/bits/stl_algo.h:2852: note: candidates are: void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student, std::allocator<Student> > >, _Compare = bool (TeamMatcher::*)(Student, Student)]

This error is repeated for the four remaining varieties. I do not understand, I am basically copying / pasting this solution here: http://www.cplusplus.com/reference/algorithm/sort/

Any help would be greatly appreciated!

+5
source share
2 answers

You need to declare yours simpleComparatoras a method static, otherwise it will not match the expected type std::sort.

To be perfectly correct, you must also pass it as TeamMatcher::simpleComparator, see here for details.

+8
source

:

bool simpleComparator(const Student& first, const Student& second){
    return (first.numberOfHrsAvailable < second.numberOfHrsAvailable);
}

, TeamMember, const .

bool Student::operator<(const Student& first, const Student& second)
{
    return (first.numberOfHrsAvailable < second.numberOfHrsAvailable);
}

, :

std::sort(studentIter.begin(), studentIter.end());

, . , :

if ( studentA < studentB )
{
    // Do stuff
}

, , (GPA, , , , IQ, ...)

+1

All Articles