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

“X 未命名類型"C++ 中的錯誤

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

問題描述

我有兩個類聲明如下:

class User
{
public:
  MyMessageBox dataMsgBox;
};

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

當我嘗試使用 gcc 編譯它時,它給出了以下錯誤:

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

MyMessageBox 沒有命名類型

MyMessageBox does not name a type

推薦答案

當編譯器編譯類 User 并到達 MyMessageBox 行時,MyMessageBox 尚未定義.編譯器不知道 MyMessageBox 存在,因此無法理解您的類成員的含義.

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 在您將其用作成員之前 已定義.這是通過顛倒定義順序來解決的.但是,您有一個循環依賴:如果您將 MyMessageBox 移到 User 上方,則在 MyMessageBox 的定義中,名稱 User 不會被定義!

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;也就是說,聲明它但不定義它.在編譯期間,已聲明但未定義的類型稱為不完整類型.考慮一個更簡單的例子:

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;
}

通過前向聲明UserMyMessageBox仍然可以形成一個指針或對它的引用:

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;
};

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

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;
};

這行不通,因為它還不知道大小.

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

附帶說明,此功能:

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

可能不應該通過指針獲取其中任何一個.你不能在沒有消息的情況下發送消息,也不能在沒有用戶的情況下發送消息.這兩種情況都可以通過將 null 作為參數傳遞給任一參數來表達(null 是一個完全有效的指針值!)

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);

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

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

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: 开云(中国)Kaiyun·官方网站 - 登录入口 | 桐城新闻网—桐城市融媒体中心主办 | 同步带轮_同步带_同步轮_iHF合发齿轮厂家-深圳市合发齿轮机械有限公司 | 搪瓷反应釜厂家,淄博搪瓷反应釜-淄博卓耀 | 伸缩节_伸缩器_传力接头_伸缩接头_巩义市联通管道厂 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 衬四氟_衬氟储罐_四氟储罐-无锡市氟瑞特防腐科技有限公司 | pos机办理,智能/扫码/二维码/微信支付宝pos机-北京万汇通宝商贸有限公司 | 影像测量仪_三坐标测量机_一键式二次元_全自动影像测量仪-广东妙机精密科技股份有限公司 | 智慧物联网行业一站式解决方案提供商-北京东成基业 | 天津热油泵_管道泵_天津高温热油泵-天津市金丰泰机械泵业有限公司【官方网站】 | 雨水收集系统厂家-雨水收集利用-模块雨水收集池-徐州博智环保科技有限公司 | 河南橡胶接头厂家,河南波纹补偿器厂家,河南可曲挠橡胶软连接,河南套筒补偿器厂家-河南正大阀门 | 国际船舶网 - 船厂、船舶、造船、船舶设备、航运及海洋工程等相关行业综合信息平台 | 太原装修公司_山西整装家装设计_太原室内装潢软装_肖邦家居 | 流量检测仪-气密性检测装置-密封性试验仪-东莞市奥图自动化科技有限公司 | 济南玻璃安装_济南玻璃门_济南感应门_济南玻璃隔断_济南玻璃门维修_济南镜片安装_济南肯德基门_济南高隔间-济南凯轩鹏宇玻璃有限公司 | 凝胶成像仪,化学发光凝胶成像系统,凝胶成像分析系统-上海培清科技有限公司 | 哈尔滨发电机,黑龙江柴油发电机组-北方星光 | 在线钠离子分析仪-硅酸根离子浓度测定仪-油液水分测定仪价格-北京时代新维测控设备有限公司 | 电竞学校_电子竞技培训学校学院-梦竞未来电竞学校官网 | 河南膏药贴牌-膏药代加工-膏药oem厂家-洛阳今世康医药科技有限公司 | 成都治疗尖锐湿疣比较好的医院-成都治疗尖锐湿疣那家医院好-成都西南皮肤病医院 | 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | UV-1800紫外光度计-紫外可见光度计厂家-翱艺仪器(上海)有限公司 | 多功能真空滤油机_润滑油全自动滤油机_高效真空滤油机价格-重庆润华通驰 | 破碎机_上海破碎机_破碎机设备_破碎机厂家-上海山卓重工机械有限公司 | 双杰天平-国产双杰电子天平-美国双杰-常熟双杰仪器 | 元拓建材集团官方网站| 诚暄电子公司首页-线路板打样,pcb线路板打样加工制作厂家 | 安平县鑫川金属丝网制品有限公司,声屏障,高速声屏障,百叶孔声屏障,大弧形声屏障,凹凸穿孔声屏障,铁路声屏障,顶部弧形声屏障,玻璃钢吸音板 | ph计,实验室ph计,台式ph计,实验室酸度计,台式酸度计 | 电动葫芦|环链电动葫芦-北京凌鹰名优起重葫芦 | 沈飞防静电地板__机房地板-深圳市沈飞防静电设备有限公司 | 洛阳装修公司-洛阳整装一站式品牌-福尚云宅装饰 | 常州企业采购平台_常州MRO采购公司_常州米孚机电设备有限公司 | 哈尔滨京科脑康神经内科医院-哈尔滨治疗头痛医院-哈尔滨治疗癫痫康复医院 | 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 大功率金属激光焊接机价格_不锈钢汽车配件|光纤自动激光焊接机设备-东莞市正信激光科技有限公司 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 软文推广发布平台_新闻稿件自助发布_媒体邀约-澜媒宝 | 振动筛-交叉筛-螺旋筛-滚轴筛-正弦筛-方形摇摆筛「新乡振动筛厂家」 |