gpt4 book ai didi

C++ 运算符 '<<' 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:28:43 26 4
gpt4 key购买 nike

我有一个关于家庭作业的问题。

我有两个类(class)。一个叫ticket.cpp,一个叫TicketOrder.cpp

主要在ticket.cpp中。

我在 Linux 上使用 g++ 编译器。

我正在做的是尝试打印出一个名为 orders 的 TicketOrder 对象的 vector ,但它给我以下错误:

ticket.cpp:57: error: no match for 'operator<<' in 'std::cout << orders. std::vector<_Tp, _Alloc>::operator[] with _Tp = TicketOrder, _Alloc = std::allocator'

这是我的代码:

门票.cpp

#include <iostream>
#include <vector>
#include <limits>
#include <cctype>
#include "TicketOrder.cpp"
using namespace std;

int main ()
{
int numberoftickets=0;
string input2;
char input3;
int profit=0;
vector <TicketOrder> orders;
int atotalmoney=0;
int btotalmoney=0;
int ctotalmoney=0;
int dtotalmoney=0;
int etotalmoney=0;

do
{
cout << "\nPick a ticket that you would like to buy: \n\n";
cout << "(A) Students without an activity card: $2.00 \n";
cout << "(B) Faculty and staff: $3.00 \n";
cout << "(C) USC alumni: $5.00 \n";
cout << "(D) UCLA students and alumni: $20.00 \n";
cout << "(E) Everyone else: $10.00 \n";

cin >> input3;

if (input3=='A')
{
cout << "How many tickets do you wish to buy? " <<endl;

if (numberoftickets >0)
{
TicketOrder order;
order.setQuantity(numberoftickets);
order.setType(input3);
orders.push_back(order);
for (int i=0; i< orders.size(); i++)
{
cout << orders[i];
}
}
}
else
{
cout << "Sorry did not recognize input, try again. " << endl;
}
} while (input3 != 'S');

TicketOrder.cpp:

#include <iostream>
using namespace std;

class TicketOrder
{
public :
//Getters

int getQuantity() const
{
return quantity;
}

char getType() const
{
return type;
}

//Setters

void setQuantity (int x)
{
quantity=x;
}

void setType(char y)
{
type =y;
}

private:
char type;
char quantity;

};

最佳答案

由于编译器笨拙地试图解释,代码是 missing an operator<< 对于 TicketOrder类。

class TicketOrder {
public:
friend std::ostream& operator<<(std::ostream& os, TicketOrder const& order) {
os << "Type: " << type << ", quantity: " << quantity;
return os;
}

char type;
int quantity;
};

(注意:您可能想将 quantity 更改为 int 。)

关于C++ 运算符 '<<' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9190673/

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