問題描述
我正在練習我的 OOP,我有以下課程:點和圓.具體來說,Circle 有一個中心點和一個半徑.相關代碼如下:
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&)
我不知道如何解釋這個錯誤.它是否告訴我需要在我的 Circle 構造函數中為 Point 參數提供 x_coord 和 y_coord?
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?
推薦答案
成員 center_pt
被默認初始化,這樣的操作將調用無參數默認構造函數 Point()代碼>.然而,這不是在
Point
類中定義的,因此會給你你得到的錯誤.
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;
}
在您可以分配給 center_pt
之前,您需要先分配一些內容.因此,在嘗試進行賦值之前,編譯器會首先嘗試為您默認初始化 center_pt
.
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)
{
}
當您創建一個類時,您實際上是在留出內存來存儲該類中的各種成員.因此,將 center_pt
和 radius_size
想象成內存中的位置,這些值存儲在類的每個實例中.當您創建一個類時,這些變量必須獲得一些默認值,如果您沒有指定任何內容,您將獲得默認構造值,無論這些值是什么.您可以稍后為這些位置分配值,但在創建類時總會進行一些初始化.如果您使用初始化列表,您可以明確指定第一次放置在內存中的內容.
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.
這篇關于沒有用于調用類構造函數的匹配函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!