我正在制作这个披萨程序,但我一直在使用矢量这个错误。它一直在说"错误:不匹配运营商<<&##;
这是我的代码
Pizza.h
#include <iostream>
#include <vector>
using namespace std;
class Order;
class Pizza
{
friend void display(Pizza,Order);
private:
double price;
char typeofPizza;
char sizeofPizza;
int toppings;
string pizzaType;
string pizzaSize;
public:
void setTypeOfPizza(char);
string getTypeOfPizza();
void setSizeOfPizza(char);
string getSizeOfPizza();
void setToppings(int);
int getToppings();
double calculate();
void loopPizza(int);
void displayPizza();
};
class Order
{
private:
vector <Pizza> customerOrder;
string customerName;
string customerNumber;
//int pizzaOrderd=1;
public:
void setName(string);
string getName();
void setNumber(string);
string getNumber();
void addPizza(Pizza);
void displayOrder(Pizza);
};
Pizza.cpp
#include <iostream>
#include "Pizza.h"
#include <vector>
using namespace std;
void Pizza::setTypeOfPizza(char t)
{
typeofPizza=t;
switch(typeofPizza)
{
case 'H': case 'h' :
pizzaType="Hand Tossed";
break;
case 'D': case 'd':
pizzaType="Deep Dish";
break;
case 'P': case 'p' :
pizzaType="Pan";
break;
default:
cout<<"Invalid size";
}
}
string Pizza::getTypeOfPizza()
{
return pizzaType;
}
void Pizza::setSizeOfPizza(char size)
{
sizeofPizza=size;
switch(sizeofPizza)
{
case 'S':
pizzaSize="Small";
break;
case 'M':
pizzaSize="Medium";
break;
case 'L':
pizzaSize="Large";
break;
default:
cout<<"Invalid size";
}
}
string Pizza::getSizeOfPizza()
{
return pizzaSize;
}
void Pizza::setToppings(int t)
{
toppings=t;
}
int Pizza::getToppings()
{
return toppings;
}
void Order::setName(string n)
{
customerName=n;
}
string Order::getName()
{
return customerName;
}
void Order::setNumber(string num)
{
customerNumber=num;
}
string Order::getNumber()
{
return customerNumber;
}
double Pizza::calculate()
{
if(sizeofPizza=='S' || sizeofPizza=='s')
{
price=10.00;
}
else if(sizeofPizza=='M' || sizeofPizza=='m')
{
price=14.00;
}
else if(sizeofPizza=='L' || sizeofPizza=='l')
{
price=17.00;
}
price+=(toppings *2);
return price;
}
void Order::addPizza(Pizza p)
{
customerOrder.push_back(p);
p.displayPizza();
cout<<"\n\n";
}
void Pizza::displayPizza()
{
cout<<"\nPizza Type: "<<getTypeOfPizza();
cout<<"\nPizza Size: "<<getSizeOfPizza();
cout<<"\nNumber of Toppings: "<<getToppings();
cout<<"\nPrice of Pizza: "<<calculate();
}
void Order::displayOrder(Pizza a)
{
int n=0;
cout<<"Name: "<<getName();
cout<<"\nNumber: "<<getNumber();
for(unsigned int i=0,n=customerOrder.size(); i<n; i++)
{
customerOrder[i]=a.displayPizza();
cout<<customerOrder[i];
}
}
的main.cpp
#include <iostream>
#include "Pizza.h"
using namespace std;
void fillvector(vector<Pizza> &);
int main()
{
char pizzaType;
char pizzaSize;
int toppings;
Pizza a;
Order b;
string number;
string name;
char choice;
cout<<"What is your name: ?\n";
getline(cin,name);
b.setName(name);
cout<<"\n\nWhat is your phone number: ?\n";
getline(cin,number);
b.setNumber(number);
cout<<"\n\nWhat type of pizza would you like?";
cout<<"\n\n(H)and tossed\t\t(D)eep Dish\t\t(P)an: ";
cin>>pizzaType;
a.setTypeOfPizza(pizzaType);
cout<<"\n\nSize of Pizza?";
cout<<"\n\n(S)mall \t\t(M)edium \t\t(L)arge: ";
cin>>pizzaSize;
a.setSizeOfPizza(pizzaSize);
cout<<"\n\nHow Many Toppings? ($2.00 a topping)";
cin>>toppings;
a.setToppings(toppings);
cout<<"\n\nWould you like to add more pizzas?Y or N\n ";
cin>>choice;
b.addPizza(a);
while(choice == 'Y' || choice == 'y')
{
cout<<"\n\nWhat type of pizza would you like?";
cout<<"\n\n(H)and tossed\t\t(D)eep Dish\t\t(P)an";
cin>>pizzaType;
a.setTypeOfPizza(pizzaType);
cout<<"\n\nSize of Pizza?";
cout<<"\n\n(S)mall \t\t(M)edium \t\t(L)arge";
cin>>pizzaSize;
a.setSizeOfPizza(pizzaSize);
cout<<"\n\nHow Many Toppings? ($2.00 a topping)";
cin>>toppings;
a.setToppings(toppings);
b.addPizza(a);
cout<<"\n\nWould you like to add more pizzas?Y or N\n ";
cin>>choice;
}
b.displayOrder(a);
}
我不知道我做错了什么我试图将displayPizza函数添加到向量中的每个点。除非我不被允许这样做。有谁能请教育我。
答案 0 :(得分:0)
在这一行:
cout << customerOrder[i];
customerOrder[i]
会返回Pizza
的引用,您尝试在operator<<
上调用cout << "type: " << customerOrder[i].getTypeOfPizza() << " size: " << customerOrder[i].getSizeOfPizza() << endl;
。你的班级没有超载。
您可以就地打印一些属性:
operator<<
或者您实施std::ostream &operator<<(std::ostream &os, Pizza const& pizza)
{
return os << pizza.getTypeOfPizza() << /* another attributes to print */;
}
以供将来使用:
{{1}}