-1

我有包含该行的代码

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>

在顶部,它毫无怨言地编译。

对此有解释吗?

(我使用的是 g++ 4.9.4,但这种行为也显示在 g++ 5.4.0 中。)

4

1 回答 1

1

std::int8_t不是内置类型。与所有其他精确宽度类型一样,它是内置类型的可选 typedef,仅当您的系统具有该宽度的适当类型时才存在。此类型和其他可用std::[u]int*_t类型在 中定义<cstdint>。因此,您需要#include <cstdint>.

正如我上面的段落所指出的,您还应该指定std::命名空间限定符,因为标头中的 stdlib 符号<c*>不需要在全局命名空间中可用。

大概<iostream>是以前间接包括<cstdint>通过某种途径,但你不应该依赖它;您应该#include为您使用的每个库符号提供正确的标题。

然后struct是未知的潜在类型的另一个主要问题引起的转移注意力;请参阅Elaborated-type-specifier for a scoped enum must not use the 'class' keyword,现在我看到它几乎与您的问题完全相同。

于 2017-08-03T11:17:32.450 回答