0

I have 2 classes, each holding private variables and initialized in their own respective constructors that use an initializer list.

The instructions for class Date states that:

In the public part of Date, add a constructor that takes a day, month, and year as input, and uses an initializer list to initialize the private variables with the passed-in values.

The instructions for class Todo_item states that:

In the public part of To_do_item, add a constructor that takes a string description and Date due date as input. Use those to initialize the variables in the private part using an initializer list. The item should always be initialized as not done.

After trying to run my program, however, I ran into an error and another problem:

cpp: In constructor ‘Todo_item::Todo_item(const string&)’:

cpp:277:32: error: no matching function for call to ‘Date::Date()’

277|    Todo_item(const string& s) {

And in the "Problems" section:

no default constructor exists for class "Date"

These problems only appeared after I created the constructor Todo_item(const string& s).

Here is my code:

1st class = class Date

class Date : public Date_base {
private:
    int day;
    int month;
    int year;
public:
    Date(int d, int m, int y) : day(d), month(m), year(y) {
        if (d >= 1 && d <=31) {
            day = d;
        } // from 1 to 31 days

        if (m >= 1 && m <=12) {
            month = m;
        } // from 1 to 12 months

        if (y >= 0) {
            year = y;
        } // 0 or higher year(s)
    } // initialize private Date variables using an initializer list

    // other methods...
};

2nd class = class Todo_item

class Todo_item : public Todo_item_base {
private:
    string description;
    Date due_date;
    bool done;
public:
    Todo_item(const string& desc, Date due) : description(desc), due_date(due) {
        done = false;
    } // initialize private Todo_item variables using an initializer list

    Todo_item(const string& s) {
        string due_date_str, state_str, description_str;
        int count = 0;

        // ... other lines of code
    }
};

I have tried to remove the const string& from Todo_item(const string& s) and just passed it without a reference (string s). I hoped that making this change in the parameter was the simple solution to this error, but this was to no avail.

What can be done to tackle those errors/problems? If you can, please include an example in your answer (it would be greatly appreciated!).

Also, let me know if further info is required.


Another note:

This is what the Todo_item(const string& s) is expected to do:

Add another constructor to Todo_item that takes a string as input. The string is formatted exactly the same as the output of Todo_item::to_string. For example, Todo_item("25/12/2018! Buy gifts") creates a Todo_item with the due date 25/12/2018, the description "Buy gifts", and a completion status of not done.

So should I be using two initializer lists in that case?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Welcome to Stack Overflow. In your own words, why doesn't `Todo_item(const string& s)` use an initializer list? What do you expect the `due_date` of the constructed object to be, before the code inside that constructor starts running? Why? – Karl Knechtel Mar 05 '23 at 03:00
  • 1
    @Gooniloon All members of a class are fully constructed *before* the constructor body is entered. So yes, you need an initializer list if any member can't be default constructed, such as `due_date`. In this case, you have 2 choices: 1) use an initializer list to *construct* `due_date` with an initial value, and then have the constructor body *update* `due_date` after parsing the string; 2) write a function to parse a string and return a `Date`, and then have the initializer list *construct* `due_date` with the result of calling that function. – Remy Lebeau Mar 05 '23 at 06:18

1 Answers1

1

Your Date class does not have a default constructor (a constructor that can be called without passing any parameter values to it).

However, the Todo_item(const string&) constructor does not have an initializer list to construct the due_date member with input values, like the Todo_item(const string&, Date) constructor does. So, the compiler needs to default-construct the due_date member instead, but it can't, hence the error.

To fix this, you need to either:

  1. add a default constructor to Date, eg:

    Date() : day(0), month(0), year(0) {}
    

    Alternatively, update the Date(int, int, int) constructor to also act as a default constructor, eg:

    Date(int d = 0, int m = 0, int y = 0) : day(d), month(m), year(y) {
        ...
    }
    
  2. add an initializer list to Todo_item(const string&) to construct the due_date member with input values, eg:

    Todo_item(const string& desc) : description(desc), due_date(0,0,0), done(false) {
        ...
    }
    
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770