2015-12-12 158 views
0

我正在製作這個比薩計劃,但我不斷收到此向量的錯誤。口口聲聲說 「錯誤:不對應的運營商< <」錯誤不符合運營商<<

這是我的代碼

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

位於文件末尾的pizza.cpp,其中for循環表示customerOrder [i] = 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 */; 
} 
+0

的屏幕截圖,我可以在for循環中執行此操作嗎? –

+0

你的代碼的第一部分 –

+0

你的意思是'cout <<「類型:」...'?是的,那是在'for'循環中。 – LogicStuff