如何解决此C++ Vector Sorting错误?

5 c++ sorting vector

这是我正在尝试编译的问题代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

然后这是我得到的错误:

TeamMatcher.C: In member function ‘void TeamMatcher::sortRosters()’:
TeamMatcher.C:51: error: no matching function for call to ‘sort(__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)]
Run Code Online (Sandbox Code Playgroud)

它为剩下的四种排序重复此错误.我不明白,我基本上是从这里复制/粘贴这个解决方案:http://www.cplusplus.com/reference/algorithm/sort/

任何帮助将不胜感激!

tza*_*man 8

您需要声明您simpleComparatorstatic方法,否则它将不符合预期的类型std::sort.

为了完全正确,你也应该把它传递给它TeamMatcher::simpleComparator,详见这里.