C++ 什么';c+;中的代码有什么问题+;涉及到一系列对象的排序?
我建议你:C++ 什么';c+;中的代码有什么问题+;涉及到一系列对象的排序?,c++,C++,我建议你: 移除原始固定大小数组并使用std::vector或 std::list,例如std::vector a 替换a[i]=人与a.emplace_back(std::move(person)) 排序范围:Sort(a.begin()、a.end()、valuecmp) 将比较函数的签名更改为接受const student&args bool valuecmp(const student&a、const student&b) 您忘记发布错误的说明部分: In file included f
std::vector
或
std::list
,例如std::vector a代码>
a[i]=人代码>与a.emplace_back(std::move(person))代码>
Sort(a.begin()、a.end()、valuecmp)代码>
const student&
args
bool valuecmp(const student&a、const student&b)
您忘记发布错误的说明部分:
In file included from /usr/include/c++/4.8.2/algorithm:62:0,
from faltu1.cpp:5:
/usr/include/c++/4.8.2/bits/stl_algo.h: In instantiation of '_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, _Compare) [with _RandomAccessIterator = student*; _Tp = student; _Compare = bool (*)(student&, student&)]':
/usr/include/c++/4.8.2/bits/stl_algo.h:2296:78: required from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = student*; _Compare = bool (*)(student&, student&)]'
表示排序
算法正试图将常量
参数传递给比较器,该比较器错误地将其参数作为非常量引用
要修复它,请将参数设置为const:
error: invalid initialization of reference of type ‘student&’ from expression
of type ‘const student’
while (__comp(*__first, __pivot))
你能告诉我们错误是什么吗?它是在编译时发生的,还是在运行时发生的?首先要弄清楚问题是在读入中还是在排序中。年龄是一个有符号的int,这是一个错误…:)我可以看到这段代码有几个地方出错,但除非你告诉我们错误是什么,否则很难帮助你。请至少包括错误的最后一行(在所有要求的
行之后),因为这会给出实际的错误消息;和前面的一行,指示是哪一行代码导致了错误。最好包括所有这些。
error: invalid initialization of reference of type ‘student&’ from expression
of type ‘const student’
while (__comp(*__first, __pivot))
bool valuecmp(student const & a, student const & b)