久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

沒有用于調用類構造函數的匹配函數

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_ptradius_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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 喷水毛片| 中文字幕1区 | www.se91 | 欧美日韩成人在线观看 | 久久精品 | 久久久久免费精品国产 | 欧美成人专区 | www国产成人免费观看视频,深夜成人网 | 久久精片 | 欧美在线视频观看 | 久久久91精品国产一区二区三区 | 91国产精品 | 成人午夜免费视频 | 一级黄色毛片a | 国产精产国品一二三产区视频 | 国产高清一区二区三区 | 欧美成人手机视频 | 日日射影院 | 福利视频一区 | 久久国产亚洲精品 | 国产精品精品久久久 | 国产成人av在线播放 | 亚洲欧美激情四射 | 欧美一区二区三区在线观看 | 九九激情视频 | 国产精品第2页 | 久久精品国产99国产精品 | 国产视频福利一区 | 色网站在线免费观看 | 91色啪 | 美女福利网站 | 亚洲三区在线观看 | 午夜亚洲 | 亚洲 自拍 另类 欧美 丝袜 | 天天干干 | 手机三级电影 | 中文字幕乱码一区二区三区 | 99久久久国产精品 | 久久久久久久久久久爱 | 中文字幕综合 | 国产做爰 |