“ X不命名类型” C ++中的错误 [英] "X does not name a type" error in C++

查看:54
本文介绍了“ X不命名类型” C ++中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个声明如下的类:

I have two classes declared as below:

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 上方,则在<$ c $的定义中c> 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!

可以做什么是转发声明 用户;也就是说,声明它但不定义它。在编译期间,已声明但未定义的类型称为不完整类型
考虑一个更简单的示例:

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

向前声明 User MyMessageBox 仍可以形成指针或对其的引用:

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.

在侧面说明中,此功能:

On a side note, this function:

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

相反,请使用引用(可能是const) :

Rather, use a reference (possibly const):

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

这篇关于“ X不命名类型” C ++中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆