2017-04-06 173 views
1

代码不能编译。我不明白是什么错误,请帮助)C++代码不能编译

#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

+1

注意紧跟在“错误”后面的词:没有匹配的函数调用'Record :: Record()' –

回答

0

你重写你的类的构造函数,所以没有一个接受零个争论,因为这需要:

Record r1; 

定义默认构造函数:

Record() {} 
+0

构造函数将构造的对象放入哪个定义的状态? –

+0

我相信Record对象的每个数据成员都将被分配,并且每个数据成员的默认构造函数都会被调用。 –

2

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

Record r1; 

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

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

或更好的重新思考你的程序的设计。

-1

main.cpp:46:12: error: no matching function for call to ‘Record::Record()’

那在这一点上:

Record r1; 

您正尝试使用默认构造函数(Record::Record)实例化对象r1。事实上,你没有提供任何参数。

此外,该编译器继续:

note: candidate expects 4 arguments, 0 provided

按照你类接口,只有这样才能实例化一个Record目的是使用所提供的唯一的构造,即:

Record(std::string, std::string, int, int); 

如果你想允许使用你必须提供的默认构造函数实例化一个Record对象。

C++ 11允许你写:

Record() = default; 

为了定义一个默认构造函数。

+0

默认构造函数很少有意义 - 在这种情况下,很难看到默认构造对象如何有用。 –

+0

@NeilButterworth如果默认的构造函数没有意义,那么现代标准就不会定义一个新的关键字来显式生成它。 –

0

你必须定义一个没有参数的构造函数。

Record r1试图呼叫Record()但它没有找到它。只需在你的类中添加额外的构造函数。它可以是空的。这也将解决与Record r3[2]相同的问题。



P.S. (与问题无关,但有帮助)

看着你的代码,我建议你查看member initializer lists来实现你的构造函数。为什么?请参阅here