关于强类型枚举前向声明
我的命名空间中有一组类,并且希望将前向声明分组到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需在通常的位置创建不透明枚举即可:
然后,无论您想要在何处定义枚举常量:
错误
opaque-enum-specifier must use a simple identifier
就是告诉您,如果您需要用简单标识符以外的方式引用枚举,你做错了。更新:
根据您的特定需求,您可以简单地在类外部声明枚举,或者在仅包含不透明枚举的简单类中声明枚举,并从中继承
Config
:You simply make the opaque enum in the usual place:
And then, wherever you want to define the enum constants:
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: