2017-03-10 56 views
-4

我讀了一些解決方案將使東西不變但我不確定的東西。或者我認爲我可能需要製作另一個構造函數?另外我得到「ld返回1退出狀態」作爲錯誤。先謝謝您的幫助 !爲什麼我的代碼得到這個錯誤兩次「未定義的參考」披薩::披薩()'「

#include <iostream> 
using namespace std; 

const int SMALL = 0; 
const int MEDIUM = 1; 
const int LARGE = 2; 

const int DEEPDISH = 0; 
const int HANDTOSSED = 1; 
const int PAN = 2; 
class Pizza{ 
    public: 
    Pizza(); 
    void setSize(int); 
    void setType(int); 
    void setCheeseToppings(int); 
    void setPepperoniToppings(int); 
    void outputDescription(){ 
     cout<<"This pizza is: "; 
     if(size==0) 
     { 
     cout<<"Small, "; 
     } 
     else if(size==1) 
     { 
     cout<<"Medium, "; 
     } 
     else 
     { 
     cout<<"Large, "; 
     } 
     if(type==0) 
     { 
     cout<<"Deep dish "; 
     } 
     else if(type==1) 
     { 
     cout<<"Hand tossed "; 
     } 
     else 
     { 
     cout<<"Pan, "; 
     } 
     cout<<"with "<<pepperoniToppings<<" pepperoni toppings and "<<cheeseToppings<<" cheese toppings."<<endl; 
    }; 
    int computePrice() 
    { 
     int total; 
     if(size==0) 
     { 
     total= 10+(pepperoniToppings+cheeseToppings)*2; 
     } 
     else if(size==1) 
     { 
     total= 14+(pepperoniToppings+cheeseToppings)*2; 
     } 
     else 
     { 
     total= 17+(pepperoniToppings+cheeseToppings)*2; 
     } 
     return total; 
    }; 
    private: 
    int size; 
    int type; 
    int cheeseToppings; 
    int pepperoniToppings; 
}; 
void Pizza::setSize(int asize){ 
    size = asize; 
} 
void Pizza::setType(int atype){ 
    type=atype; 
} 
void Pizza::setCheeseToppings(int somegoddamncheesetoppings){ 
    cheeseToppings = somegoddamncheesetoppings; 
} 
void Pizza::setPepperoniToppings(int thesefuckingpepperonis){ 
    pepperoniToppings = thesefuckingpepperonis; 
} 

int main() 
{ 
Pizza cheesy; 
Pizza pepperoni; 

cheesy.setCheeseToppings(3); 
cheesy.setType(HANDTOSSED); 
cheesy.outputDescription(); 
cout << "Price of cheesy: " << cheesy.computePrice() << endl; 

pepperoni.setSize(LARGE); 
pepperoni.setPepperoniToppings(2); 
pepperoni.setType(PAN); 
pepperoni.outputDescription(); 
cout << "Price of pepperoni : " << pepperoni.computePrice() << endl; 
return 0; 
} 
+0

請張貼您的代碼。 – bejado

+0

請發佈_less_ code,即** [mcve] ** – Tas

回答

2

您聲明構造函數Pizza()但沒有實現它。無論是實現它還是不聲明它,以便編譯器爲您生成默認構造函數。

1

您聲明瞭一個構造函數Pizza();,但從來沒有定義它。當實例化兩個Pizza對象時,鏈接器嘗試解析對構造函數的隱式調用時,它無法找到它。

嘗試Pizza() = default;如果沒有什麼特別的事情需要在構造函數中完成。

相關問題