No matching function for call to [class]

47,899

Solution 1

This is because you've tried to copy an object and there is no default constructor supplied.

this->date = date;

What you should be doing, is initializing everything in the initializer list. There is also no reason you shouldn't be passing some of these arguments by reference.

Payment(int amount, const string& name, const Date& date)
    : amount(amount)
    , name(name)
    , date(date)
    {}

Same goes for your Date class. This will use the compiler generated copy constructor. Be aware that if your class contains more then POD types, you might want to implement your own copy constructor.

Solution 2

Because you have

Date date;

in your Payment class, you must have a Date constructor that takes no arguments, i.e. Date::Date(), which you do not have specified.

Share:
47,899

Related videos on Youtube

Admin
Author by

Admin

Updated on July 03, 2020

Comments

  • Admin
    Admin almost 4 years

    So, I have some classes that I was defining, and it gave me errors for this:

    #include <iostream>
    using namespace std;;
    
    //void makepayment(int amount, string name, Date date);
    //void listpayments();
    
    class Date;
    
    class Date {
      public:
        int month;
        int day;
        int year;
        Date(int month, int day, int year) {
          this->month = month;
          this->day = day;
          this->year = year;
        }
    };
    
    class Payment {
      public:
        int amount;
        string name;
        Date date;
        Payment(int amount, string name, Date date) {
          this->amount = amount;
          this->name = name;
          this->date = date;
        }
    };
    
    int main() {
    cout <<
    "|~~~~~~~~~~~~~~~~~~~~~~~~| \n" <<
    "|   WELCOME TO THE       | \n" <<
    "|   WESSLES BANK         | \n" <<
    "|   MANAGEMENT SYSTEM!   | \n" <<
    "|~~~~~~~~~~~~~~~~~~~~~~~~| \n";
    
    for(;;) {
    
    }
    
    return 0;  
    }
    

    The error was:

    foo.cpp: In constructor ‘Payment::Payment(int, std::string, Date)’:
    foo.cpp:26:49: error: no matching function for call to ‘Date::Date()’
    foo.cpp:26:49: note: candidates are:
    foo.cpp:14:5: note: Date::Date(int, int, int)
    foo.cpp:14:5: note:   candidate expects 3 arguments, 0 provided
    foo.cpp:9:7: note: Date::Date(const Date&)
    foo.cpp:9:7: note:   candidate expects 1 argument, 0 provided
    

    I have no idea what is wrong! What does 'no matching function for call' mean?

    Sorry if this is a noobie question... I just started c++.

Related