No matching function for call to Class Constructor

前端 未结 5 1264
感动是毒
感动是毒 2020-12-09 17:03

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:



        
相关标签:
5条回答
  • 2020-12-09 17:35

    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.

    0 讨论(0)
  • 2020-12-09 17:35

    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

    0 讨论(0)
  • 2020-12-09 17:40

    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.

    0 讨论(0)
  • 2020-12-09 17:43

    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)
    {
    
    }
    
    0 讨论(0)
  • 2020-12-09 17:47

    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.

    0 讨论(0)
提交回复
热议问题