关于强类型枚举前向声明

发布于 2024-12-08 20:06:20 字数 947 浏览 0 评论 0原文

我的命名空间中有一组类,并且希望将前向声明分组到 Define.hpp 文件中。

我已经在其他项目中实现了这一点,这是一个示例:

namespace Makefile
{
    class Builder;
    class Config;
    class Options;
    class Target;
    class Tool;
}

我的问题是 - 例如 - Config 类包含以下强类型枚举:

namespace Makefile
{
    class Config
    {   
    public:
    enum class OperatingSystem : unsigned int 
        {   
            MacOSX = 0,
            Linux = 1,
            Windows = 2 
        };  
    };
}

所以我想在我的前向声明文件中添加此枚举类,并且添加以下行:

enum class Config::OperatingSystem : unsigned int;

我的问题来了,编译时出现以下错误:

src/Makefile/define.hpp:6:13: error: opaque-enum-specifier must use a simple identifier

这是否意味着无法前向声明嵌套类型? 这个问题有什么解决办法吗? 这是由于我的编译器造成的吗?

我的配置:Mac OS X.7 上的 GCC 4.6.0

编译标志:--std=c++0x -g3 -gdwarf-2 -W -Wall -iquote gen -iquote src

I've a set of classes in my namespace and want to group forward declaration in a define.hpp file.

I've already achieve this in others projects, here is an example :

namespace Makefile
{
    class Builder;
    class Config;
    class Options;
    class Target;
    class Tool;
}

My problem is that — for example — the Config class include the following strongly typed enum :

namespace Makefile
{
    class Config
    {   
    public:
    enum class OperatingSystem : unsigned int 
        {   
            MacOSX = 0,
            Linux = 1,
            Windows = 2 
        };  
    };
}

So I would like to add this enum class in my forward-declaration file, and add the following line :

enum class Config::OperatingSystem : unsigned int;

Here comes my problem, I get the following error when compiling :

src/Makefile/define.hpp:6:13: error: opaque-enum-specifier must use a simple identifier

Does it mean that it is not possible to forward-declare nested type?
Is there any workaround to this problem?
Is this due to my compiler?

My configuration : GCC 4.6.0 on Mac OS X.7

Compilation flags : --std=c++0x -g3 -gdwarf-2 -W -Wall -iquote gen -iquote src

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

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

发布评论

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

评论(1

不顾 2024-12-15 20:06:20

您只需在通常的位置创建不透明枚举即可:

namespace Makefile
{
    class Config
    {   
    public:
    enum class OperatingSystem : unsigned int;
    };
}

然后,无论您想要在何处定义枚举常量:

namespace Makefile
{
    enum Config::OperatingSystem : unsigned int
    {   
        MacOSX = 0,
        Linux = 1,
        Windows = 2 
    };
}

错误opaque-enum-specifier must use a simple identifier就是告诉您,如果您需要用简单标识符以外的方式引用枚举,你做错了。

更新
根据您的特定需求,您可以简单地在类外部声明枚举,或者在仅包含不透明枚举的简单类中声明枚举,并从中继承 Config

namespace Makefile
{
    class ConfigDecl
    {
     public:
        enum class OperatingSystem : unsigned int;
    };
    class Config : public ConfigDecl;
}

You simply make the opaque enum in the usual place:

namespace Makefile
{
    class Config
    {   
    public:
    enum class OperatingSystem : unsigned int;
    };
}

And then, wherever you want to define the enum constants:

namespace Makefile
{
    enum Config::OperatingSystem : unsigned int
    {   
        MacOSX = 0,
        Linux = 1,
        Windows = 2 
    };
}

The error opaque-enum-specifier must use a simple identifier is there to tell you that if you need to refer to the enum with other than a simple identifier, you are doing it wrong.

UPDATE:
For your specific needs, you may simply declare the enum outside of the class, or else in a simple class with only the opaque enum, and inherit Config from it:

namespace Makefile
{
    class ConfigDecl
    {
     public:
        enum class OperatingSystem : unsigned int;
    };
    class Config : public ConfigDecl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文