No matching function for call to Class Constructor

前端 未结 5 1468
感动是毒
感动是毒 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: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.

提交回复
热议问题