【问题标题】:'enum struct' won't compile unless <iostream> is included [duplicate]除非包含<iostream>,否则'enum struct'不会编译[重复]
【发布时间】:2017-08-03 11:15:29
【问题描述】:

我有包含该行的代码

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 中。)

【问题讨论】:

    标签: c++11 struct enums include enum-class


    【解决方案1】:

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

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

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

    那么关于struct 的事情是由未知底层类型的另一个主要问题引起的红鲱鱼;请参阅Elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword,现在我看到它几乎与您的问题完全相同。

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 2013-11-30
      • 2014-01-13
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多