0

我正在尝试对一个数组进行排序,其中包含按字母表填充的字符串值。它已经适用于整数。我认为问题不在于排序,而在于显示它。

但这里是代码:

#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 说:二进制表达式的操作数无效

谢谢你和最好的问候弗洛

4

1 回答 1

4

可能没有什么问题sort。显然错误的是你没有ostream& operator<<for 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;
}
于 2013-07-04T20:29:54.553 回答