2015-03-02 102 views
0

的類內初始化我有這段代碼不能在g ++下編譯 - 4.9.1(我使用了命令「g ++ -c --std = C++ 11 map.cc 「)std :: map

#include <map> 
#include <cstdint> 
class A { 
    std::map<uint8_t, uint8_t> b = std::map<uint8_t, uint8_t>(); 
}; 

編譯時我收到以下錯誤:

map.cc:5:52: error: expected ‘;’ at end of member declaration 
    std::map<uint8_t, uint8_t> b = std::map<uint8_t, uint8_t>(); 
                ^
map.cc:5:52: error: declaration of ‘std::map<unsigned char, unsigned char> A::uint8_t’ [-fpermissive] 
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.9/include/stdint.h:9:0, 
       from /usr/include/c++/4.9/cstdint:41, 
       from /usr/include/c++/4.9/bits/char_traits.h:380, 
       from /usr/include/c++/4.9/string:40, 
       from /usr/include/c++/4.9/stdexcept:39, 
       from /usr/include/c++/4.9/array:38, 
       from /usr/include/c++/4.9/tuple:39, 
       from /usr/include/c++/4.9/bits/stl_map.h:63, 
       from /usr/include/c++/4.9/map:61, 
       from map.cc:1: 
/usr/include/stdint.h:48:24: error: changes meaning of ‘uint8_t’ from ‘typedef unsigned char uint8_t’ [-fpermissive] 
typedef unsigned char uint8_t; 
         ^
map.cc:5:59: error: expected unqualified-id before ‘>’ token 
    std::map<uint8_t, uint8_t> b = std::map<uint8_t, uint8_t>(); 
                 ^
map.cc:5:43: error: wrong number of template arguments (1, should be 4) 
    std::map<uint8_t, uint8_t> b = std::map<uint8_t, uint8_t>(); 
             ^
In file included from /usr/include/c++/4.9/map:61:0, 
       from map.cc:1: 
/usr/include/c++/4.9/bits/stl_map.h:96:11: error: provided for ‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’ 
    class map 
     ^

但是,如果我與詮釋更換uint8_t,它編譯罰款。

+0

這似乎是一個GCC的問題。 – chris 2015-03-02 06:16:52

+0

謝謝。它似乎在clang ++下編譯(Ubuntu的clang版本爲3.5.0-4ubuntu2)。我應該搜索g ++的bug報告。 – Anirudh 2015-03-02 06:22:14

+4

請注意,因爲您擁有它,所以課堂上的初始化是毫無意義的,因爲您只是在執行默認初始化,這將在沒有任何幫助的情況下發生。 – 2015-03-02 06:24:18

回答

0

FWIW,如果你需要一個解決辦法,下面的工作:

class A { 
    typedef std::map<uint8_t, uint8_t> B; 
    B b = B(); 
}; 
+0

沒關係,使用gcc 4.8 – 2015-03-02 06:28:04

+0

感謝您的建議。對於我的具體問題,有幾個解決方法,比如像Jerry指出的,完全省略初始化。 Fwiw,它不適用於gcc-4.8/g ++ - 4.8。 – Anirudh 2015-03-02 06:38:41