gpt4 book ai didi

C++ 代码无法编译

转载 作者:行者123 更新时间:2023-11-30 05:14:56 24 4
gpt4 key购买 nike

代码无法编译。我不明白错误是什么,请帮助)

#include <iostream>
#include <fstream>

class Record{
std::string product_name;
std::string product_category;
int price;
int amount;
public:
Record(std::string newName, std::string newCategory, int newPrice, int newAmount){
product_name=newName;
product_category=newCategory;
price=newPrice;
amount=newAmount;
}

std::string getName(){
return product_name;
}
std::string getCategory(){
return product_category;
}
int getPrice(){
return price;
}
int getAmount(){
return amount;
}
void setName(std::string newName){
product_name=newName;
}
void setCategory(std::string newCategory){
product_category=newCategory;
}
void setPrice(int newPrice){
price=newPrice;
}
void setAmount(int newAmount){
amount=newAmount;
}
};

int main(){
Record r1;
r1.setName("beer");
r1.setCategory("alcohol");
r1.setPrice(12);
r1.setAmount(32);
Record r2("carrot", "vegetables", 123, 1932);
std::cout<<r1.getName()<<" "<<r1.getCategory()<<" "<<r1.getPrice()<<" "<<r1.getAmount()<< std::endl;
std::cout<<r2.getName()<<" "<<r2.getCategory()<<" "<<r2.getPrice()<<" "<<r2.getAmount()<< std::endl;
Record r3[2];
std::string a;
float b;
unsigned int c;
for(unsigned int i=0; i<2; ++i){
std::cout<<"input name: ";
std::cin>>a;
r3[i].setName(a);
std::cout<<"input category: ";
std::cin>>a;
r3[i].setCategory(a);
std::cout<<"input price: ";
std::cin>>b;
r3[i].setPrice(b);
std::cout<<"input amount: ";
std::cin>>c;
r3[i].setAmount(c);
}
for(unsigned int i=0; i<2; ++i){
std::cout<<r3[i].getName()<<" "<<r3[i].getCategory()<<" "<<r3[i].getPrice()<<" "<<r3[i].getAmount()<< std::endl;

}

return 0;

}

Error text: g++ -Wall -c "main.cpp" ( /media/ad/4GB-NTFS/prog/laba2) main.cpp: In function ‘int main()’: main.cpp:46:12: error: no matching function for call to ‘Record::Record()’ Record r1; ^ main.cpp:12:1: note: candidate: Record::Record(std::__cxx11::string, std::__cxx11::string, int, int) Record(std::string newName, std::string newCategory, int newPrice, int newAmount){ ^ main.cpp:12:1: note: candidate expects 4 arguments, 0 provided main.cpp:6:7: note: candidate: Record::Record(const Record&) class Record{ ^ main.cpp:6:7: note: candidate expects 1 argument, 0 provided main.cpp:54:16: error: no matching function for call to ‘Record::Record()’ Record r3[2]; ^ main.cpp:12:1: note: candidate: Record::Record(std::__cxx11::string, std::__cxx11::string, int, int) Record(std::string newName, std::string newCategory, int newPrice, int newAmount){ ^ main.cpp:12:1: note: candidate expects 4 arguments, 0 provided main.cpp:6:7: note: candidate: Record::Record(const Record&) class Record{ ^ main.cpp:6:7: note: candidate expects 1 argument, 0 provided

最佳答案

您的类没有默认构造函数。所以当你说:

   Record r1;

编译器不知道如何创建r1 对象。您要么需要在创建 r 时提供所有参数:

  Record r1( "foo", "bar", 1, 2 );

或者更好地完全重新考虑您的程序设计。

关于C++ 代码无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43267174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com