为什么我的代码两次收到此错误"undefined reference to `Pizza::Pizza()' "

Why is my code getting this error twice "undefined reference to `Pizza::Pizza()' "

本文关键字:Pizza reference undefined to 代码 我的 两次 为什么 错误      更新时间:2023-10-16

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

您声明了构造函数Pizza(),但没有实现它。要么实现它或不声明它,因此编译器为您生成默认构造函数。

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

尝试Pizza() = default;如果在构造函数中没有什么特别的要做。

相关文章: