gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:24:40 25 4
gpt4 key购买 nike

如果我这样定义我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com