为什么我的代码两次得到此错误“未定义引用`Pizza :: Pizza()'”

时间:2017-03-10 05:23:11

标签: c++

我读了一些解决方案会使某些东西成为常数但我不确定的东西。或者我认为我可能需要制作另一个构造函数?另外我得到“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;
}

2 个答案:

答案 0 :(得分:2)

您声明了构造函数Pizza()但未实现它。要么实现它,要么不声明它,以便编译器为您生成默认构造函数。

答案 1 :(得分:1)

您声明了构造函数Pizza();但从未定义它。当链接器在实例化两个Pizza对象时尝试解析对构造函数的隐式调用时,它无法找到它。

如果构造函数中没有任何特殊内容,请尝试Pizza() = default;