No matching function for call to Class Constructor(没有用于调用类构造函数的匹配函数)
问题描述
我正在练习我的 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.
这篇关于没有用于调用类构造函数的匹配函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:没有用于调用类构造函数的匹配函数
基础教程推荐
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17