No matching function for call to Class Constructor

限于喜欢 提交于 2019-12-17 19:48:38

问题


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?


回答1:


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.




回答2:


Change the constructor the following way

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

The problem is that if you will not call explicitly the constructor with parameters for class Point then the compiler tries to call the default constructor of the class to create data member center_pt of class Circle before you can assign the point within the body of the constructor of class Circle. But you did not define the default constructor of class Point and the compiler issued an error.

The other approach is indeed to define the default constructor for class Point that for example initilaizes a point with zeroes.

Take into account that by design of class Point you can not change data members coord_x and coord_y of a created object.Maybe you should redesign the class.




回答3:


This is because you are not initializing the member in the initialization list, but rather assigning to it in the body. Consequently, it is first constructing it using the default ctor, and hence your compiler's complaint.

Consider changing things to:

// Circle.cpp
Circle::Circle(const Point& center, double radius) :
    center_pt(center),
    radius_size(radius)
{

}



回答4:


Change

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

to

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

So that it uses the constructor that you have defined for point. Also better for radius as well




回答5:


In trying to construct Circle, you tried to construct a Point using a default constructor:

Circle::Circle(const Point& center, double radius)
                     ^
                //...Calling default constructor...

...and then assigned it a value:

center_pt = center;

Given that the default constructor didn't exist, you got the compile-time error.

Two solutions are:

1) Construct a Point using a member initialisation list:

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

2) Define a default constructor for Point:

Point::Point(){

}

I am primarily answering this question to place emphasis on option 2 as I believe this emphasis was lacking in the answers above. Sometimes, it is more practical to default construct an object in a givens class parameter list and assign it a value at a later stage. This is encountered a bit in GUI programming in using the Qt framework.



来源:https://stackoverflow.com/questions/31211319/no-matching-function-for-call-to-class-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!