代码无法编译。我不明白错误是什么,请帮忙)
#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;
}
错误文本:g++ -Wall -c "main.cpp" (/media/ad/4GB-NTFS/prog/laba2) main.cpp: 在函数 'int main()': main.cpp:46:12: 错误:没有用于调用“Record::Record()”Record r1 的匹配函数;^ main.cpp:12:1: 注意: 候选: 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: 注意:候选人需要 4 个参数,0 提供 main.cpp:6:7: 注意:候选人:Record::Record(const Record&) class Record{ ^ main.cpp:6:7:注意:候选人需要 1 个参数,0 提供 main.cpp:54:16:错误:没有匹配的函数调用“Record::Record()”记录 r3[2];^ main.cpp:12:1: 注意: 候选: Record::Record(std::__cxx11::string, std::__cxx11::string, int, int) Record(std::string newName, std::string新类别,int newPrice,
你已经覆盖了你的类的构造函数,所以没有一个接受零参数的,因为这需要:
Record r1;
定义一个默认构造函数:
Record() {}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句