0

In this code, I would like to use an object of class birthday, the constructors of class Date are:

    Date(unsigned int y, unsigned int m, unsigned int d);
    Date(string yearMonthDay); // yearMonthDay must be in format "yyyy/mm/dd"

But I'm getting an error: "No default constructor exist for class Date"

class Person {

public:
    Person(std::string name, char gender, Date birthday);
    string getName();
    char getGender();
    int getYear();
    int getMonth();
    int getDay();

private:
    Date birthday;
    std::string name;
    char gender;
};

Person::Person(std::string name, char gender, Date birthday){
    Person::name = name;
    Person::gender = gender;
    Person::birthday = birthday;
}

Class Date:

class Date {
public:
    Date(unsigned int y, unsigned int m, unsigned int d);
    Date(string yearMonthDay); // yearMonthDay must be in format "yyyy/mm/dd"
    void setYear(unsigned int y);
    void setMonth(unsigned int m);
    void setDay(unsigned int d);
    void setDate(unsigned int y, unsigned int m, unsigned int d);
    unsigned int getYear() const;
    unsigned int getMonth() const;
    unsigned int getDay() const;
    string getDate() const; // returns the date in format "yyyy/mm/dd"

private:
    unsigned int year;
    unsigned int month;
    unsigned int day;
};

2 Answers 2

3

Use a constructor initializer list:

Person::Person(std::string name, char gender, Date birthday){
    : name(name), gender(gender), birthday(birthday)
{
    // Empty
}

That will initialize the members only once using the arguments, calling the appropriate constructors of the members.

Without a constructor initializer list, the members will be default initialized (default constructed) and then in the body of the constructor you use normal assignment to the members.

Sign up to request clarification or add additional context in comments.

Comments

1

When you write any of your own constructors, the no-argument constructor is no longer supplied by the compiler. You can reinstate it (ask for it to be supplied) though:

Date() = default;

To make sure that you can default construct a Date safely, you should give proper defaults to its members:

private:
    unsigned int year{2000};
    unsigned int month{4};
    unsigned int day{30};

5 Comments

But only if it makes sense to do this. Which it probably doesn't.
True, I only fixed the symptom in this case. But I don't know if the answer is wrong per se, so I'll leave it up.
@AsteroidsWithWings you'd be surprised how often a default ctor is needed, e.g. allocating an array of Dates or Persons.
@franji1 You'd be surprised how often a default ctor isn't needed. Why are you using new[]?
A date is a date. It shouldn't be a not-date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.