我有一个关于家庭作业的问题.
我有两节课.一个叫做ticket.cpp,另一个叫做TicketOrder.cpp
主要在ticket.cpp内.
我在Linux上使用g ++编译器.
我正在做的是尝试打印出一个TicketOrder名为orders 的对象的向量,但它给了我以下错误:
ticket.cpp:57:错误:'std :: cout <<命令中的'operator <<'不匹配.std :: vector <_Tp,_Alloc> :: operator [],_Tp = TicketOrder,_Alloc = std :: allocator'
这是我的代码:
#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');
Run Code Online (Sandbox Code Playgroud)
#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;
};
Run Code Online (Sandbox Code Playgroud)
由于编译器笨拙地试图解释,代码缺少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;
};
Run Code Online (Sandbox Code Playgroud)
(注意:您可能想要更改quantity为int.)