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

“X 未命名類型"C++ 中的錯(cuò)誤

quot;X does not name a typequot; error in C++(“X 未命名類型C++ 中的錯(cuò)誤)
本文介紹了“X 未命名類型"C++ 中的錯(cuò)誤的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有兩個(gè)類聲明如下:

class User
{
public:
  MyMessageBox dataMsgBox;
};

class MyMessageBox
{
public:
  void sendMessage(Message *msg, User *recvr);
  Message receiveMessage();
  vector<Message> *dataMessageList;
};

當(dāng)我嘗試使用 gcc 編譯它時(shí),它給出了以下錯(cuò)誤:

When I try to compile it using gcc, it gives the following error:

MyMessageBox 沒(méi)有命名類型

MyMessageBox does not name a type

推薦答案

當(dāng)編譯器編譯類 User 并到達(dá) MyMessageBox 行時(shí),MyMessageBox 尚未定義.編譯器不知道 MyMessageBox 存在,因此無(wú)法理解您的類成員的含義.

When the compiler compiles the class User and gets to the MyMessageBox line, MyMessageBox has not yet been defined. The compiler has no idea MyMessageBox exists, so cannot understand the meaning of your class member.

您需要確保 MyMessageBox 在您將其用作成員之前 已定義.這是通過(guò)顛倒定義順序來(lái)解決的.但是,您有一個(gè)循環(huán)依賴:如果您將 MyMessageBox 移到 User 上方,則在 MyMessageBox 的定義中,名稱 User 不會(huì)被定義!

You need to make sure MyMessageBox is defined before you use it as a member. This is solved by reversing the definition order. However, you have a cyclic dependency: if you move MyMessageBox above User, then in the definition of MyMessageBox the name User won't be defined!

你能做的是forward declare User;也就是說(shuō),聲明它但不定義它.在編譯期間,已聲明但未定義的類型稱為不完整類型.考慮一個(gè)更簡(jiǎn)單的例子:

What you can do is forward declare User; that is, declare it but don't define it. During compilation, a type that is declared but not defined is called an incomplete type. Consider the simpler example:

struct foo; // foo is *declared* to be a struct, but that struct is not yet defined

struct bar
{
    // this is okay, it's just a pointer;
    // we can point to something without knowing how that something is defined
    foo* fp; 

    // likewise, we can form a reference to it
    void some_func(foo& fr);

    // but this would be an error, as before, because it requires a definition
    /* foo fooMember; */
};

struct foo // okay, now define foo!
{
    int fooInt;
    double fooDouble;
};

void bar::some_func(foo& fr)
{
    // now that foo is defined, we can read that reference:
    fr.fooInt = 111605;
    fr.foDouble = 123.456;
}

通過(guò)前向聲明UserMyMessageBox仍然可以形成一個(gè)指針或?qū)λ囊?

By forward declaring User, MyMessageBox can still form a pointer or reference to it:

class User; // let the compiler know such a class will be defined

class MyMessageBox
{
public:
    // this is ok, no definitions needed yet for User (or Message)
    void sendMessage(Message *msg, User *recvr); 

    Message receiveMessage();
    vector<Message>* dataMessageList;
};

class User
{
public:
    // also ok, since it's now defined
    MyMessageBox dataMsgBox;
};

不能反過(guò)來(lái)這樣做:如前所述,類成員需要有一個(gè)定義.(原因是編譯器需要知道User占用了多少內(nèi)存,并且知道它需要知道其成員的大小.)如果你說(shuō):

You cannot do this the other way around: as mentioned, a class member needs to have a definition. (The reason is that the compiler needs to know how much memory User takes up, and to know that it needs to know the size of its members.) If you were to say:

class MyMessageBox;

class User
{
public:
    // size not available! it's an incomplete type
    MyMessageBox dataMsgBox;
};

這行不通,因?yàn)樗€不知道大小.

It wouldn't work, since it doesn't know the size yet.

附帶說(shuō)明,此功能:

 void sendMessage(Message *msg, User *recvr);

可能不應(yīng)該通過(guò)指針獲取其中任何一個(gè).你不能在沒(méi)有消息的情況下發(fā)送消息,也不能在沒(méi)有用戶的情況下發(fā)送消息.這兩種情況都可以通過(guò)將 null 作為參數(shù)傳遞給任一參數(shù)來(lái)表達(dá)(null 是一個(gè)完全有效的指針值!)

Probably shouldn't take either of those by pointer. You can't send a message without a message, nor can you send a message without a user to send it to. And both of those situations are expressible by passing null as an argument to either parameter (null is a perfectly valid pointer value!)

相反,使用引用(可能是常量):

Rather, use a reference (possibly const):

 void sendMessage(const Message& msg, User& recvr);

這篇關(guān)于“X 未命名類型"C++ 中的錯(cuò)誤的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉(zhuǎn)置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對(duì)壓縮(鎖定)容器進(jìn)行排序)
Rotating a point about another point (2D)(圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)一個(gè)點(diǎn) (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識(shí)別的算法改進(jìn))
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構(gòu)建 ISO 8601 日期時(shí)間?)
Sort list using STL sort function(使用 STL 排序功能對(duì)列表進(jìn)行排序)
主站蜘蛛池模板: 精密冲床,高速冲床等冲压设备生产商-常州晋志德压力机厂 | 防爆暖风机_防爆电暖器_防爆电暖风机_防爆电热油汀_南阳市中通智能科技集团有限公司 | 青岛空压机,青岛空压机维修/保养,青岛空压机销售/出租公司,青岛空压机厂家电话 | 上海办公室设计_办公楼,写字楼装修_办公室装修公司-匠御设计 | 算命免费_生辰八字_免费在线算命 - 卜算子算命网 | 旗帜网络笔记-免费领取《旗帜网络笔记》电子书 | 蓝牙音频分析仪-多功能-四通道-八通道音频分析仪-东莞市奥普新音频技术有限公司 | 铝板冲孔网,不锈钢冲孔网,圆孔冲孔网板,鳄鱼嘴-鱼眼防滑板,盾构走道板-江拓数控冲孔网厂-河北江拓丝网有限公司 | 体检车_移动CT车_CT检查车_CT车_深圳市艾克瑞电气有限公司移动CT体检车厂家-深圳市艾克瑞电气有限公司 | 盐城网络公司_盐城网站优化_盐城网站建设_盐城市启晨网络科技有限公司 | 数控车床-立式加工中心-多功能机床-小型车床-山东临沂金星机床有限公司 | 整合营销推广|营销网络推广公司|石家庄网站优化推广公司|智营销 好物生环保网、环保论坛 - 环保人的学习交流平台 | 湖州织里童装_女童男童中大童装_款式多尺码全_织里儿童网【官网】-嘉兴嘉乐网络科技有限公司 | 陕西自考报名_陕西自学考试网| 医院专用门厂家报价-医用病房门尺寸大全-抗菌木门品牌推荐 | 电梯乘运质量测试仪_电梯安全评估测试仪-武汉懿之刻 | 全自动面膜机_面膜折叠机价格_面膜灌装机定制_高速折棉机厂家-深圳市益豪科技有限公司 | 登车桥动力单元-非标液压泵站-非标液压系统-深圳市三好科技有限公司 | 牛奶检测仪-乳成分分析仪-北京海谊| 成都软件开发_OA|ERP|CRM|管理系统定制开发_成都码邻蜀科技 | 暖气片十大品牌厂家_铜铝复合暖气片厂家_暖气片什么牌子好_欣鑫达散热器 | 北京易通慧公司从事北京网站优化,北京网络推广、网站建设一站式服务商-北京网站优化公司 | 北京森语科技有限公司-模型制作专家-展览展示-沙盘模型设计制作-多媒体模型软硬件开发-三维地理信息交互沙盘 | 舞台木地板厂家_体育运动木地板_室内篮球馆木地板_实木运动地板厂家_欧氏篮球地板推荐 | 耐火砖厂家,异形耐火砖-山东瑞耐耐火材料厂 | 细沙回收机-尾矿干排脱水筛设备-泥石分离机-建筑垃圾分拣机厂家-青州冠诚重工机械有限公司 | 智能楼宇-楼宇自控系统-楼宇智能化-楼宇自动化-三水智能化 | 全国冰箱|空调|洗衣机|热水器|燃气灶维修服务平台-百修家电 | 小青瓦丨古建筑瓦丨青瓦厂家-宜兴市徽派古典建筑材料有限公司 | 花纹铝板,合金铝卷板,阴极铝板-济南恒诚铝业有限公司 | BOE画框屏-触摸一体机-触控查询一体机-触摸屏一体机价格-厂家直销-触发电子 | 合肥花魁情感婚姻咨询中心_挽回爱情_修复婚姻_恋爱指南 | GEDORE扭力螺丝刀-GORDON防静电刷-CHEMTRONICS吸锡线-上海卓君电子有限公司 | 超声波电磁流量计-液位计-孔板流量计-料位计-江苏信仪自动化仪表有限公司 | SMC-SMC电磁阀-日本SMC气缸-SMC气动元件展示网 | 碳纤维复合材料制品生产定制工厂订制厂家-凯夫拉凯芙拉碳纤维手机壳套-碳纤维雪茄盒外壳套-深圳市润大世纪新材料科技有限公司 | 新疆十佳旅行社_新疆旅游报价_新疆自驾跟团游-新疆中西部国际旅行社 | 【官网】博莱特空压机,永磁变频空压机,螺杆空压机-欧能优 | 卫浴散热器,卫浴暖气片,卫生间背篓暖气片,华圣格浴室暖气片 | 宁波普瑞思邻苯二甲酸盐检测仪,ROHS2.0检测设备,ROHS2.0测试仪厂家 | 水热合成反应釜-防爆高压消解罐-西安常仪仪器设备有限公司 |