没有匹配的函数来调用类构造函数 [英] No matching function for call to Class Constructor

查看:269
本文介绍了没有匹配的函数来调用类构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在练习我的OOP,并且有以下课程:点和圆。具体来说,圆有一个中心点和一个半径。以下是相关代码:

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 被默认初始化,这样的操作将调用no参数默认构造函数 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.

这篇关于没有匹配的函数来调用类构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆