Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No matching function for call to Class Constructor

Tags:

I am practicing my OOP and I have the following classes: Point and Circle. Specifically, Circle has a center Point, and a radius. Here is the relevant code:

// Point.h class Point {     public:         Point(double x, double y);         double x() const;         double y() const;         std::string as_string() const;      private:         double x_coord;         double y_coord; };  // Circle.h class Circle {     public:         Circle(const Point& center, double radius);         Point center() const;         double radius() const;         std::string as_string() const;         std::string equation() const;      private:         Point center_pt;         double radius_size; };  // Circle.cpp Circle::Circle(const Point& center, double radius) {     center_pt = center;     radius_size = radius; } 

However, when I try to compile this code, I get the following error:

Circle.cpp: In constructor ‘Circle::Circle(const Point&, double)’: Circle.cpp:3: error: no matching function for call to ‘Point::Point()’ Point.h:10: note: candidates are: Point::Point(double, double) Point.h:8: note:                 Point::Point(const Point&) 

I am not sure how to interpret this error. Is it telling me I need to provide the x_coord and y_coord for the Point parameter in my Circle constructor?

like image 685
nonremovable Avatar asked Jul 03 '15 16:07

nonremovable


1 Answers

The member center_pt is being default initialized and such an operation will call the no arguments default constructor Point(). This however is not defined in the Point class and therefore gives you the error you got.

Circle::Circle(const Point& center, double radius) {     center_pt = center; //<-- this is an assignment                         //default init has already occurred BEFORE this point     radius_size = radius; } 

Before you can assign to center_pt here you need something to assign to. The compiler therefore tries to default initialize center_pt for you first before trying to do the assignment.

Instead if you use the member initializer list you can avoid the problem of the default construction followed by assignment:

Circle::Circle(const Point& center, double radius):     center_pt(center),     radius_size(radius) { } 

When you create a class you are essentially setting aside the memory to store the various members within that class. So imagine center_pt and radius_size as places in the memory that those values get stored in for each instance of your class. When you create a class those variables have to get given some default values, if you don't specify anything you get the default constructed values, whatever those are. You can assign values later to those locations but some initialization will always occur at the time of class creation. If you use the initializer list you get to explicitly specify what gets placed in the memory the first time around.

By using the member initializer list here your members are being constructed appropriately the first time around. It also has the benefit of saving some unnecessary operations.

like image 93
shuttle87 Avatar answered Sep 29 '22 18:09

shuttle87