2016-07-18 10 views
1

const unordered_mapを宣言したいが、コンパイラエラーが発生し、正しい構文がわからない。私が見つけたすべての例ですが、1つは非constです。私が知っている唯一のconstの例と同じようにしていますが、コンパイラエラーが発生します。ここに私のコードは次のとおりです。"const unordered_map"の使い方は?

110対
typedef const std::unordered_map<std::string,int> StringIntMap; 
StringIntMap string_to_case { 
    {"get",1}, 
    {"add",2} 
}; 

は私を与える:

Error 1 error C2601: 'string_to_case' : local function definitions are illegal 

または私はstring_to_case間の等号を入れて、私が手ブラケットの場合:120_CTP_Nov2012対

Error 1 error C2552: 'string_to_case' : non-aggregates cannot be initialized with initializer list 

ではなくなります私は等号の有無にかかわらず次のようにします:

error C2440: 'initializing' : cannot convert from 'initializer-list' to 'std::unordered_map<std::string,int,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' 
1>   with 
1>   [ 
1>    _Kty=std::string 
1> ,   _Ty=int 
1>   ] 
1>   No constructor could take the source type, or constructor overload resolution was ambiguous 

何が間違っていますか?

答えて

1

これは、GCC 6.1.1で動作します:

$ cat t.C 
#include <unordered_map> 
#include <string> 

typedef const std::unordered_map<std::string,int> StringIntMap; 
StringIntMap string_to_case = { 
    {"get",1}, 
    {"add",2} 
}; 
$ g++ -g -c -std=c++1z -o t.o t.C 
$ g++ --version 
g++ (GCC) 6.1.1 20160621 (Red Hat 6.1.1-3) 

あなたは何も悪いことをやっていません。あなたはどちらかコンパイラのバグを打つか、あなたのコンパイラは最新のC++標準をサポートしていません。

+0

ありがとうございますが、ビジュアルスタジオ(少なくともvs2012アップデート5とvs2012 CTPノベル2012)、どちらも受け付けません:( – AbsolutelyFreeWeb

+2

合意。VS 15以降(コンパイラバージョン19)が必要です。 –

+0

スペアボックスにFedora Linuxの最新バージョンをインストールするには約1時間かかります.gcc 6.1.1はそのままの状態で使用できます。最新のC++標準をサポートする最新のC++コンパイラを使用するには、 –

関連する問題