C++为什么我无法使用在声明的类之外全局声明的枚举?

发布于 2024-09-02 16:32:50 字数 529 浏览 3 评论 0原文

现在,我的项目有两个类和一个主类。由于这两个类相互继承,因此它们都使用前向声明。在第一个对象中,在 #include 语句的正下方,我在类定义之前初始化了两个枚举。我可以在该类中很好地使用这两个枚举。但是,如果我尝试在继承第一个类的另一个类中使用这些枚举,则会收到一条错误消息,指出枚举尚未声明。如果我尝试在第二类中重新定义枚举,则会收到重新定义错误。

我什至尝试使用我刚刚读到的技巧,并将每个枚举放在自己的名称空间中;这并没有改变任何事情。

这是一个示例:

#ifndef CLASSONE_H
#define CLASSONE_H

namespace Player
{
    enum Enum
    {
        One,
        Two,
    };
}

#endif

然后在第二个类中,我尝试使用之前声明的枚举:

void AddPlayer(Player::Enum playerNumber);

却收到一条错误消息,指出“玩家”尚未声明。

Right now, my project has two classes and a main. Since the two classes inherit from each other, they are both using forward declarations. In the first object, right underneath the #include statement, I initialize two enums, before the class definition. I can use both enums just fine inside that class. However, if I try to use those enums in the other class, which inherits from the first one, I get an error saying the enum has not been declared. If I try to redefine the enum in the second class, I get a redefinition error.

I have even tried using a trick I just read about, and putting each enum in its own namespace; that didn't change anything.

Here's an example:

#ifndef CLASSONE_H
#define CLASSONE_H

namespace Player
{
    enum Enum
    {
        One,
        Two,
    };
}

#endif

Then inside the second class, I attempt to use the enum declared earlier:

void AddPlayer(Player::Enum playerNumber);

and instead get an error saying 'Player' has not been declared.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

壹場煙雨 2024-09-09 16:32:50

我不确定您在没有看到代码的情况下遇到了什么问题,但是这可以编译:

enum OutsideEnum
{
    OE_1,
    OE_2,
};

namespace ns
{
    enum NSEnum
    {
       NE_1,
       NE_2,
    };
}

class Base
{
public:
    enum BaseEnum
    {
        BE_1,
        BE_2,
    };

    void BaseFunc();
};

class Derived
{
public:
    enum DerivedEnum
    {
        DE_1,
        DE_2,
    };

    void DerivedFunc();
};

void Base::BaseFunc()
{
    BaseEnum be = BE_1;
    Derived::DerivedEnum de = Derived::DE_1;
    OutsideEnum oe = OE_1;
    ns::NEEnum ne = ns::NE_1;
}

void Derived::DerivedFunc()
{
    Base::BaseEnum be = Base::BE_1;
    DerivedEnum de = DE_1;
    OutsideEnum oe = OE_1;
    ns::NEEnum ne = ns::NE_1;
}

int main()
{
    Base::BaseEnum be = Base::BE_1;
    Derived::DerivedEnum de = Derived::DE_1;
    OutsideEnum oe = OE_1;
    ns::NEEnum ne = ns::NE_1;
}

在类定义中定义的枚举需要注意两件事:

  1. 如果您希望它公开可用,请确保将其声明为公开。
  2. 当从定义它的类以外的任何地方引用它时,请使用类名来限定枚举和值的名称。

编辑:

好的,问题与枚举无关,而是包含的顺序,当您有基类和派生类时,只有派生类需要了解基类:

基类标头:

#ifndef BASE_H
#define BASE_H

enum BaseEnum
{
};

class Base
{
};
#endif

派生类标头:

#ifndef DERIVED_H
#define DERIVED_H

#include "Base.h"

class Derived
{

   void Func(BaseEnum be);
};
#endif

I'm not sure what issue you are having without seeing your code, but this compiles:

enum OutsideEnum
{
    OE_1,
    OE_2,
};

namespace ns
{
    enum NSEnum
    {
       NE_1,
       NE_2,
    };
}

class Base
{
public:
    enum BaseEnum
    {
        BE_1,
        BE_2,
    };

    void BaseFunc();
};

class Derived
{
public:
    enum DerivedEnum
    {
        DE_1,
        DE_2,
    };

    void DerivedFunc();
};

void Base::BaseFunc()
{
    BaseEnum be = BE_1;
    Derived::DerivedEnum de = Derived::DE_1;
    OutsideEnum oe = OE_1;
    ns::NEEnum ne = ns::NE_1;
}

void Derived::DerivedFunc()
{
    Base::BaseEnum be = Base::BE_1;
    DerivedEnum de = DE_1;
    OutsideEnum oe = OE_1;
    ns::NEEnum ne = ns::NE_1;
}

int main()
{
    Base::BaseEnum be = Base::BE_1;
    Derived::DerivedEnum de = Derived::DE_1;
    OutsideEnum oe = OE_1;
    ns::NEEnum ne = ns::NE_1;
}

Two things to watch for with enums defined inside a class definition:

  1. Make sure it's declared public if you want it publicly available.
  2. When referencing it from anywhere other than the class it's defined in, use the class name to qualify the name of the enum and the values.

EDIT:

Ok, the problem has nothing to do with enums, but rather order of inclusion, when you have a base class and a derived class, only the derived class needs to know about the base class:

Base class header:

#ifndef BASE_H
#define BASE_H

enum BaseEnum
{
};

class Base
{
};
#endif

Derived class header:

#ifndef DERIVED_H
#define DERIVED_H

#include "Base.h"

class Derived
{

   void Func(BaseEnum be);
};
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文