Being very new to c++, I have come across 2 different ways of using a constructor and I am confused on why one of them works, and the other doesen't.
class Journey {
protected:
Coordinate start;
// this constructor wont work
public:
Journey(Coordinate startIn)
{
start = startIn;
}
}
- This constructor does not work as I get the error: constructor for 'Journey' must explicitly initialise the member 'start' which does not have a default constructor
public:
Journey(Coordinate startIn): start(startIn){
}
- whereas this constructor works perfectly fine
- Also keep in mind "Coordinate" is just a class I created
I am not sure what the reason is as I thought the 1st way of initialising variables works for all cases, so I just need an explanation why this isn't the case here. I tried looking around for the answer without any success.