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 轉換單一顏色)
主站蜘蛛池模板: 细石混凝土泵_厂家_价格-烟台九达机械有限公司 | 必胜高考网_全国高考备考和志愿填报信息平台 | 回转支承-转盘轴承-回转驱动生产厂家-洛阳隆达轴承有限公司 | 广州展览设计公司_展台设计搭建_展位设计装修公司-众派展览装饰 广州展览制作工厂—[优简]直营展台制作工厂_展会搭建资质齐全 | 次氯酸钠厂家,涉水级次氯酸钠,三氯化铁生产厂家-淄博吉灿化工 | 高博医疗集团上海阿特蒙医院 | 青海电动密集架_智能密集架_密集架价格-盛隆柜业青海档案密集架厂家 | 油漆辅料厂家_阴阳脚线_艺术漆厂家_内外墙涂料施工_乳胶漆专用防霉腻子粉_轻质粉刷石膏-魔法涂涂 | 石家庄律师_石家庄刑事辩护律师_石家庄取保候审-河北万垚律师事务所 | 魔方网-培训咨询服务平台| VOC检测仪-甲醛检测仪-气体报警器-气体检测仪厂家-深恒安科技有限公司 | 石家庄小程序开发_小程序开发公司_APP开发_网站制作-石家庄乘航网络科技有限公司 | 魔方网-培训咨询服务平台| 物和码官网,物和码,免费一物一码数字化营销SaaS平台 | 安徽净化板_合肥岩棉板厂家_玻镁板厂家_安徽科艺美洁净科技有限公司 | 丁基胶边来料加工,医用活塞边角料加工,异戊二烯橡胶边来料加工-河北盛唐橡胶制品有限公司 | 塑料撕碎机_编织袋撕碎机_废纸撕碎机_生活垃圾撕碎机_废铁破碎机_河南鑫世昌机械制造有限公司 | 中药二氧化硫测定仪,食品二氧化硫测定仪|俊腾百科 | PC构件-PC预制构件-构件设计-建筑预制构件-PC构件厂-锦萧新材料科技(浙江)股份有限公司 | 原子吸收设备-国产分光光度计-光谱分光光度计-上海光谱仪器有限公司 | 胶泥瓷砖胶,轻质粉刷石膏,嵌缝石膏厂家,腻子粉批发,永康家德兴,永康市家德兴建材厂 | 无尘烘箱_洁净烤箱_真空无氧烤箱_半导体烤箱_电子防潮柜-深圳市怡和兴机电 | 通风天窗,通风气楼,屋顶通风天窗,屋顶通风天窗公司 | 天津次氯酸钠酸钙溶液-天津氢氧化钠厂家-天津市辅仁化工有限公司 | 上海诺狮景观规划设计有限公司| PE一体化污水处理设备_地埋式生活污水净化槽定制厂家-岩康塑业 | 物流公司电话|附近物流公司电话上门取货 | 小型高低温循环试验箱-可程式高低温湿热交变试验箱-东莞市拓德环境测试设备有限公司 | 航空障碍灯_高中低光强航空障碍灯_民航许可认证航空警示灯厂家-东莞市天翔航天科技有限公司 | 恒压供水控制柜|无负压|一体化泵站控制柜|PLC远程调试|MCGS触摸屏|自动控制方案-联致自控设备 | 呼末二氧化碳|ETCO2模块采样管_气体干燥管_气体过滤器-湖南纳雄医疗器械有限公司 | 电动葫芦|手拉葫芦|环链电动葫芦|微型电动葫芦-北京市凌鹰起重机械有限公司 | 太空舱_民宿太空舱厂家_移动房屋太空舱价格-豪品建筑 | 武汉印刷厂-不干胶标签印刷厂-武汉不干胶印刷-武汉标签印刷厂-武汉标签制作 - 善进特种标签印刷厂 | 石英陶瓷,石英坩埚,二氧化硅陶瓷-淄博百特高新材料有限公司 | 上海深蓝_缠绕机_缠膜机-上海深蓝机械装备有限公司 | 安平县鑫川金属丝网制品有限公司,声屏障,高速声屏障,百叶孔声屏障,大弧形声屏障,凹凸穿孔声屏障,铁路声屏障,顶部弧形声屏障,玻璃钢吸音板 | 轻型地埋电缆故障测试仪,频响法绕组变形测试仪,静荷式卧式拉力试验机-扬州苏电 | 液压中心架,数控中心架,自定心中心架-烟台恒阳机电设计有限公司 行星搅拌机,双行星搅拌机,动力混合机,无锡米克斯行星搅拌机生产厂家 | 京马网,京马建站,网站定制,营销型网站建设,东莞建站,东莞网站建设-首页-京马网 | 济南保安公司加盟挂靠-亮剑国际安保服务集团总部-山东保安公司|济南保安培训学校 |