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

“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 在您將其用作成員之前 已定義.這是通過顛倒定義順序來解決的.但是,您有一個循環(huán)依賴:如果您將 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仍然可以形成一個指針或?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;
};

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

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

可能不應該通過指針獲取其中任何一個.你不能在沒有消息的情況下發(fā)送消息,也不能在沒有用戶的情況下發(fā)送消息.這兩種情況都可以通過將 null 作為參數(shù)傳遞給任一參數(shù)來表達(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);

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

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉(zhuǎn)一個點 (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++ 中構(gòu)建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: а_天堂中文最新版地址 | 无码一区二区三区视频 | 久久一区二区免费视频 | 99视频在线免费观看 | 色综合成人网 | 91久久久久久久久 | 一二三四在线视频观看社区 | 无人区国产成人久久三区 | 久久伊人精品 | 成在线人视频免费视频 | 亚洲美女视频 | 亚洲欧美日韩精品久久亚洲区 | 日本大香伊一区二区三区 | 欧美精品一区二区在线观看 | 嫩草视频网 | 6996成人影院网在线播放 | 日本不卡一区二区三区在线观看 | 天天av综合 | 搞av.com | 欧美日韩精品免费观看 | 欧美国产视频 | 中文二区 | 久久尤物免费一区二区三区 | 特级黄一级播放 | 99在线免费观看视频 | 久久久久久亚洲国产精品 | a级在线免费视频 | 97精品超碰一区二区三区 | 91传媒在线观看 | 怡红院成人在线视频 | 日本在线一区二区 | 日韩欧美国产精品一区二区 | 一级毛片黄片 | 国产1区2区 | 久久综合一区 | 国产99久久精品一区二区永久免费 | 99久久精品国产一区二区三区 | 国产在线91| 国产视频一区二区 | 亚洲免费成人av | 亚洲国产一区二区三区 |