2016-07-18 9 views
1

const unordered_map을 선언하고 싶지만 컴파일러 오류가 발생하며 올바른 구문을 알아낼 수 없습니다. 모든 예제는 찾았지만 하나는 const가 아니며 내가 아는 유일한 const 예제와 똑같은 방식으로 수행하고 있지만 컴파일러 오류가 발생합니다."const unordered_map"을 사용하는 방법은 무엇입니까?

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

또는 내가 string_to_case과 브라켓 사이에 등호를 넣을 경우 내가 얻을 :

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

대 (110)가 나에게 제공합니다

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

이 120_CTP_Nov2012 대신주는 대 여기 내 코드입니다 나와 동등한 기호가 있거나없는 다음 사람 :

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

고맙습니다.하지만 Visual Studio (적어도 vs2012 업데이트 5 및 vs2012 CTP Nov 2012)에 액세스 할 수 없습니다. ( – AbsolutelyFreeWeb

+2

동의합니다 .VS 15 이상 (컴파일러 버전 19)이 필요합니다. –

+0

gcc 6.1.1과 함께 여분의 상자에 Fedora Linux의 최신 버전을 설치하는 데 약 한 시간이 걸립니다. 최신 C++ 표준을 지원하는 최신 C++ 컴파일러를 사용하려면 –