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 astring
description andDate
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?