2

The code does not compile. I do not understand what the error is, help please)

#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

4 Answers4

2

Your class doesn't have a default constructor. So when you say:

   Record r1;

the compiler doesn't know how to create the r1 object. You either need to provide all the parameters when r is created:

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

or better completely rethink the design of your program.

0

You have overriden the constructor of your class, and so there is not one that accepts zero arguments as this requires:

Record r1;

Define a default constructor:

Record() {}
Chad K
  • 184
  • 1
  • 9
  • What defined state does that constructor place the constructed object in? –  Apr 06 '17 at 23:31
  • I believe each data member of the Record object will be allocated and the default constructor of each data member called. – Chad K Apr 06 '17 at 23:44
0

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

That is, at this point:

Record r1;

You are trying to instantiate the object r1 with a default constructor (Record::Record). Indeed you are not providing any parameter.

Moreover the compiler continues:

note: candidate expects 4 arguments, 0 provided

Following your class interface, the only way to instantiate a Record object is to use the only constructor provided, that is:

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

If you want to allow to instantiate an Record object with a default constructor you have to provide it.

C++11 allows you to write:

Record() = default;

In order to define a default constructor.

BiagioF
  • 9,368
  • 2
  • 26
  • 50
  • A default constructor rarely makes sense - in this case it's hard to see how a default constructed object would be useful. –  Apr 06 '17 at 23:33
  • @NeilButterworth if default constructor didn't make sense, the modern standard would not have defined a new keyword in order to explicitly generate it. – BiagioF Apr 07 '17 at 14:23
0

You have to define a constructor with no parameters.

Record r1 tries to call Record() but it doesn't find it. Just add the extra constructor in your class. It can be empty. This will also solve the same problem with Record r3[2].



P.S. (unrelated to the question, but helpful)

Looking at your code, I suggest you check out member initializer lists for implementing your constructors. Why? See here.

Community
  • 1
  • 1
5ar
  • 2,069
  • 10
  • 27