为什么我的代码两次收到此错误"undefined reference to `Pizza::Pizza()' "
Why is my code getting this error twice "undefined reference to `Pizza::Pizza()' "
我读了一些东西,解决方案会使某些事情成为常数,但我不确定。或者我认为我可能需要制作另一个构造函数?另外,我将" 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;
如果在构造函数中没有什么特别的要做。
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 不断"Attempting to reference a deleted function"
- std::iterator::reference 必须是引用吗?
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- C++返回类型 T(&)[] 与使用 reference = T(&)[] 作为返回类型
- const auto & 和 auto & if reference 对象之间的区别是 const
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- 以前的'namespace reference { }'声明
- build error : undefined reference to `yyFlexLexer::yyFlexLex
- C ++引用函数参数似乎包含原始对象的副本,而不是充当"real reference"
- 为什么我的代码两次收到此错误"undefined reference to `Pizza::Pizza()' "