No matching function for call to Class Constructor

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

提交回复
热议问题