c++ - 为什么我不能定义没有常量的比较

标签 c++ compiler-construction g++

如果我这样定义我的 compare 函数:

bool compare(Student& a, Student& b)
{
    return a.n < b.n;
}

g++ 会报错:

g++ -Wall main.cpp -o main
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/algorithm:63:0,
                 from main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h: In function ‘_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >, _Tp = Student, _Compare = bool (*)(Student&, Student&)]’:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:2261:78:   instantiated from ‘_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >, _Compare = bool (*)(Student&, Student&)]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:2302:62:   instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >, _Size = long int, _Compare = bool (*)(Student&, Student&)]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:5250:4:   instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >, _Compare = bool (*)(Student&, Student&)]’
main.cpp:38:51:   instantiated from here
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:2229:4: error: invalid initialization of reference of type ‘Student&’ from expression of type ‘const Student’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:2232:4: error: invalid initialization of reference of type ‘Student&’ from expression of type ‘const Student’

Compilation exited abnormally with code 1 at Mon May 28 08:05:35

但是如果我用 const 类型定义比较,它将编译并正常工作。

这里是所有的代码:

class Student {
public:
    Student(string);
    string n;

};

bool compare(Student& a, Student& b)
{
    return a.n < b.n;
}

Student::Student(string name) { n = name; }

int main()
{
    Student A = Student("A");
    Student B = Student("B");

    vector<Student> students;
    students.push_back(B);
    students.push_back(A);

    sort(students.begin(), students.end(), compare);

    cout << "After sort" << endl;
    for(vector<Student>::iterator i = students.begin(); i != students.end(); ++i) {
        cout << "Student: " << i->n << endl;
    }

    return 0;
}

最佳答案

在此实现中,std::sort 使用

 const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare);

在您的例子中,_Tp 是学生,_Comparecompare

所以你基本上有

const Student& std::__median(const Student&, const Student&, const Student&, 
                                                   bool (*)(Student&, Student&) )

或类似的。显然,回调不能应用于参数,因为它们被转换为 const,所以失败。

将参数设为您的compare 方法const

关于c++ - 为什么我不能定义没有常量的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10776931/

相关文章:

STL - C++ "no matching function for call to"结构错误

c++ - 从跳过列表中删除节点

c++ - 如何将 std::chrono::time_point 转换为带有小数秒的日历日期时间字符串?

compiler-construction - Erlang 代码替换

linux - 安装 FMOD Ex for Linux 以在 g++ 中使用的说明在哪里或什么?

c++ - 无法在Debian Linux中编译C++ 17

c++ - 使用连接的套接字终止 QWebSocketServer

c++ - 使用 QPen 绘制带有虚线图案的多色虚线

c - 对于这个简单的程序,词法分析后的输出是什么?

Bison 的冲突