2017-08-03 40 views
-1

我有一個包含「枚舉結構」將無法編譯,除非<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顯示這種行爲。)

回答

1

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,現在我看它,幾乎與您的問題完全相同。