Hi I'm just wondering why I get an error with this code
the error is line 8 :no matching function for call to birthday::birthday()'
birthday.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ifndef BIRTHDAY_H
#define BIRTHDAY_H
class birthday
{
public:
birthday(int day,int month,int year);
void sayBirthday();
private:
int d;
int m;
int y;
};
#endif // BIRTHDAY_H
Since you have defined a constructor for birthday, there is no default constructor.
Since class person contains an object of type birthday, and the person constructor doesn't explicitly construct the birthday object, the compiler will attempt to construct it with the default constructor. But there is none, so you get the error.
One way is to call the existing constructor in the person constructor:
1 2 3
person::person() :
b(12,25,2000) // construct b with birthday(int day ,int month ,int year)
{}
Another is to add a default constructor to class birthday
1 2 3 4 5 6 7 8 9 10 11 12
class birthday
{
public:
birthday() : d(25), m(12), y(2000) {}
birthday(int day,int month,int year);
void sayBirthday();
private:
int d;
int m;
int y;
};
A third is to declare default parameters to the existing birthday constructor:
1 2 3 4 5 6 7 8 9 10 11
class birthday
{
public:
birthday(int day=25,int month=12,int year=2000);
void sayBirthday();
private:
int d;
int m;
int y;
};