Ask coding questions

← Back to all posts
No default constructor exists
Coder100 (17027)

Click the RUN button.

Why is it doing that? I am assigning a value! Is there a way to make a default value?

Answered by finlay44111 (78) [earned 5 cycles]
View Answer
finlay44111 (78)

the way cpp works, is that if you dont initialize a member in the member initialization list, it gets a default constructor
so here is the new constructor

ThreeDPoint(Point p, int z) :
    p(p, z)

if you dont have the member initialization list, it will automatically call the default constructor for all the members, like this

ThreeDPoint(Point p, int z) :

and since Point has no default constructor (constructor with no arguments) defined, it will throw an error.

You could also fix this error by adding a default constructor to Point like so

Point() = default;

in the public scope of your class