【发布时间】:2012-01-19 05:23:20
【问题描述】:
这是我试图编译的问题代码:
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);
}
然后这是我得到的错误:
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)]
它对剩余的四种排序重复此错误。我不明白,我基本上是从这里复制/粘贴这个解决方案:http://www.cplusplus.com/reference/algorithm/sort/
任何帮助将不胜感激!
【问题讨论】:
-
您是否将 using namespace 指令
using namespace std;添加到您的文件中,如果没有,您需要将std命名空间的算法名称质量为std::sort。 -
我做到了,至少在我导入到这个 .C 文件的 .h 文件中。那还是不错的吧?
-
@Als - 您可以从编译器输出中看到它已经将
std::sort视为候选对象,这意味着这不是问题。 -
这不是问题,但也不是很好,不要在头文件中添加 using 指令 这是一种不好的做法。它将该命名空间中的所有符号导入到包含标题的翻译单元中。这会导致符号名称污染,相信我
std命名空间有很多你不需要的东西。另外,这可能会导致编译时间更长.在源 cpp 文件中使用 using 声明。 -
@tzaman:我在 Joachim 编辑 Q 以使错误可读之前发布了评论。这是第一次预感没有看到不可读的错误消息。这有时被称为 心理调试 的问题。另外,由于这只是一个猜测,我将其发布为评论而不是回答。