“枚举结构”将无法编译,除非包括
问题描述:
我有一个包含“枚举结构”将无法编译,除非<iostream>包括
enum struct cols: int8_t {red, blue, green};
当我编译,我得到错误的行代码:
test.cpp:4:1: warning: elaborated-type-specifier for a scoped enum must not use the 'struct' keyword
enum struct cols: int8_t {red, blue, green};
^
test.cpp:4:13: error: use of enum 'cols' without previous declaration
enum struct cols: int8_t {red, blue, green};
^
test.cpp:4:17: error: expected unqualified-id before ':' token
enum struct cols: int8_t {red, blue, green};
^
但是,如果我把行
#include <iostream>
在顶部,它编译没有投诉。
对此有解释吗?
(我用克++ 4.9.4,但也与克++ 5.4.0显示这种行为。)
答
std::int8_t
不是内置型。与所有其他精确宽度类型一样,它是可选的内置类型的typedef,仅当系统具有适当类型的宽度时才存在。这个和其他可用的std::[u]int*_t
类型在<cstdint>
中定义。因此,您需要#include <cstdint>
。
如上面的段落所示,您还应该指定std::
名称空间限定符,因为<c*>
标头中的stdlib符号不需要在全局名称空间中可用。
大概<iostream>
之前通过某些路线间接包括<cstdint>
,但您不应该依赖于此;你应该使用#include
正确的标题符号。
然后关于struct
的东西是从另一个未知基础类型的主要问题产生的红色鲱鱼;请参阅Elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword,现在我看它,几乎与您的问题完全相同。