pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

沒有用于調用類構造函數(shù)的匹配函數(shù)

No matching function for call to Class Constructor(沒有用于調用類構造函數(shù)的匹配函數(shù))
本文介紹了沒有用于調用類構造函數(shù)的匹配函數(shù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在練習我的 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;
}

但是,當我嘗試編譯此代碼時,出現(xiàn)以下錯誤:

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 構造函數(shù)中為 Point 參數(shù)提供 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 被默認初始化,這樣的操作將調用無參數(shù)默認構造函數(shù) 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)
{
}

當您創(chuàng)建一個類時,您實際上是在留出內存來存儲該類中的各種成員.因此,將 center_ptradius_size 想象成內存中的位置,這些值存儲在類的每個實例中.當您創(chuàng)建一個類時,這些變量必須獲得一些默認值,如果您沒有指定任何內容,您將獲得默認構造值,無論這些值是什么.您可以稍后為這些位置分配值,但在創(chuàng)建類時總會進行一些初始化.如果您使用初始化列表,您可以明確指定第一次放置在內存中的內容.

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.

通過在此處使用成員初始值設定項列表,您的成員將在第一次被正確構建.它還具有節(jié)省一些不必要操作的好處.

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.

這篇關于沒有用于調用類構造函數(shù)的匹配函數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關文檔推薦

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 函數(shù))
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 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 安徽净化工程设计_无尘净化车间工程_合肥净化实验室_安徽创世环境科技有限公司 | 宿松新闻网 宿松网|宿松在线|宿松门户|安徽宿松(直管县)|宿松新闻综合网站|宿松官方新闻发布 | 雨水收集系统厂家-雨水收集利用-模块雨水收集池-徐州博智环保科技有限公司 | 旅游规划_旅游策划_乡村旅游规划_景区规划设计_旅游规划设计公司-北京绿道联合旅游规划设计有限公司 | 折弯机-刨槽机-数控折弯机-数控刨槽机-数控折弯机厂家-深圳豐科机械有限公司 | 耐压仪-高压耐压仪|徐吉电气| 仿清水混凝土_清水混凝土装修_施工_修饰_保护剂_修补_清水混凝土修复-德州忠岭建筑装饰工程 | 莱州网络公司|莱州网站建设|莱州网站优化|莱州阿里巴巴-莱州唯佳网络科技有限公司 | 盘装氧量分析仪-防爆壁挂氧化锆分析仪-安徽吉帆仪表有限公司 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 车间除尘设备,VOCs废气处理,工业涂装流水线,伸缩式喷漆房,自动喷砂房,沸石转轮浓缩吸附,机器人喷粉线-山东创杰智慧 | 上海瑶恒实业有限公司|消防泵泵|离心泵|官网 | 智能交通网_智能交通系统_ITS_交通监控_卫星导航_智能交通行业 | 中央空调温控器_风机盘管温控器_智能_液晶_三速开关面板-中央空调温控器厂家 | 精密光学实验平台-红外粉末压片机模具-天津博君 | 雷冲击高压发生器-水内冷直流高压发生器-串联谐振分压器-武汉特高压电力科技有限公司 | 达利园物流科技集团- | 雷蒙磨,雷蒙磨粉机,雷蒙磨机 - 巩义市大峪沟高峰机械厂 | 空冷器|空气冷却器|空水冷却器-无锡赛迪森机械有限公司[官网] | 超声波清洗机_细胞破碎仪_实验室超声仪器_恒温水浴-广东洁盟深那仪器 | 博莱特空压机|博莱特-阿特拉斯独资空压机品牌核心代理商 | 全自动实验室洗瓶机,移液管|培养皿|进样瓶清洗机,清洗剂-广州摩特伟希尔机械设备有限责任公司 | 镀锌角钢_槽钢_扁钢_圆钢_方矩管厂家_镀锌花纹板-海邦钢铁(天津)有限公司 | 石英砂矿石色选机_履带辣椒色选机_X光异物检测机-合肥幼狮光电科技 | 干式磁选机_湿式磁选机_粉体除铁器-潍坊国铭矿山设备有限公司 | 特种阀门-调节阀门-高温熔盐阀-镍合金截止阀-钛阀门-高温阀门-高性能蝶阀-蒙乃尔合金阀门-福建捷斯特阀门制造有限公司 | 电主轴-高速精密电主轴-高速电机厂家-瑞德沃斯品牌有限公司 | 蜘蛛车-高空作业平台-升降机-高空作业车租赁-臂式伸缩臂叉装车-登高车出租厂家 - 普雷斯特机械设备(北京)有限公司 | 带式过滤机厂家_价格_型号规格参数-江西核威环保科技有限公司 | 吊篮式|移动式冷热冲击试验箱-二槽冷热冲击试验箱-广东科宝 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 污水处理设备,一体化泵站,一体化净水设备-「梦之洁环保设备厂家」 | 杭州画室_十大画室_白墙画室_杭州美术培训_国美附中培训_附中考前培训_升学率高的画室_美术中考集训美术高考集训基地 | 诸城网站建设-网络推广-网站优化-阿里巴巴托管-诸城恒泰互联 | 合肥网带炉_安徽箱式炉_钟罩炉-合肥品炙装备科技有限公司 | 环球电气之家-中国专业电气电子产品行业服务网站! | 明渠式紫外线杀菌器-紫外线消毒器厂家-定州市优威环保 | 广东西屋电气有限公司-广东西屋电气有限公司| ORP控制器_ORP电极价格-上优泰百科 | 热处理炉-退火炉-回火炉设备厂家-丹阳市电炉厂有限公司 | 广州昊至泉水上乐园设备有限公司 |