note: 'person::person()' is implicitly deleted because the default definition would be ill-formed
Solution 1:
Well, the problem is not with that "note". The "note" simply explains the reason for the error. The error is that you are trying to default-construct your person
object when class person
does not have a default constructor.
Instead of trying to default-construct it, you can {}
- initialize that const member and the code will compile
person bob = { nextPersonID++, "Bob", {}, 1 };
bob.birthdate.day = 1;
bob.birthdate.month = 1;
bob.birthdate.year = 1990;
...
Alternatively, you can simply write your own default constructor for the class.
Solution 2:
The problem has not to do with the default-constructor. There is a constant in the class declaration but the default (no argument) constructor does not guarantee that the constant will be defined. Suggest using an "initializer list".
struct Person {
int id;
string name;
date birthdate;
const int numberOfAddresses;
address addresses [1];
Person(int); // constructor declaration
Person() : numberOfAddresses(1) {} // constructor definition.
// ": numberOfAddresses(1)" is the initializer list
// ": numberOfAddresses(1) {}" is the function body
};
Person::Person(int x) : numberOfAddresses(x) {} // constructor definition. ": numberOfAddresses{x}" is the initializer list
int main()
{
Person Bob; // calls Person::Person()
Person Shurunkle(10); // calls Person::Person(int)
}