我正在尝试使用字母表填充字符串值来对数组进行排序。它已经适用于整数。我认为问题不在于排序,而在于显示它。
但这是代码:
#include <iostream>
#include <algorithm>
using namespace std;
class Ticket {
int ticketnr;
string name;
public:
Ticket() {
ticketnr = 0;
name = "NN";
};
Ticket(int _tickernr, string _name) {
ticketnr = _tickernr;
name = _name;
}
friend bool upSort(Ticket a, Ticket b);
};
bool upSort(Ticket a, Ticket b) {
return (a.name > b.name);
}
int main() {
Ticket vip(1435, "Beckenbauer");
Ticket frei;
Ticket array[10] = {vip, Ticket(2100, "Maier")};
sort(array, array + 10, upSort);
for (int i = 0; i < 10; i++) cout << array[i] << endl;
}
Xcode说:二进制表达式的操作数无效
谢谢你,最诚挚的问候 FLO
答案 0 :(得分:4)
sort
可能没有错。明显错误的是,ostream& operator<<
没有Ticket
,所以你不能这样做:
for (int i = 0; i < 10; i++) cout << array[i] << endl;
// ^^^^^^^^^^^^^^^^
所以,
friend
std::ostream& operator<<(std::ostream& o, const Ticket& t)
{
return o << t.ticketnr << " " << t.name;
}