発生している問題
std::mapの値を取得したいのですが以下のソースですとエラーになってしまいます。
取得側のconstをつけたり外したり,参照をやめたり、いろいろ試したのですが解決しません。
何かお気づきの方がいらっしゃいましたら教えてほしいです。
VC++17を使用
C++
1void test( void ) 2{ 3 static const std::map<std::string, std::vector<int>> tMap ={ 4 { "AAA", { 0, 1, 2, 3 } }, 5 { "BBB", { 1, 2, 3, 4 } } 6 }; 7 8 const std::vector<int>& test1 = tMap["AAA"]; // <---- エラーになる 9}
エラーメッセージ
error C2678: 二項演算子 '[': 型 'const std::map<std::string,std::vector<_type,std::allocator<int>>,std::lessstd::string,std::allocator<std::pair<const std::string,std::vector<_type,std::allocator<int>>>>>' の左オペランドを扱う演算子が見つかりません (または変換できません) (新しい動作; ヘルプを参照)。
with
[
_type=int
]
message : 'std::vector<_type,std::allocator<int>> &std::map<std::string,std::vector<_type,std::allocator<int>>,std::lessstd::string,std::allocator<std::pair<const std::string,std::vector<_type,std::allocator<int>>>>>::operator [](const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' の可能性があります
with
[
_type=int
]
message : または 'std::vector<_type,std::allocator<int>> &std::map<std::string,std::vector<_type,std::allocator<int>>,std::lessstd::string,std::allocator<std::pair<const std::string,std::vector<_type,std::allocator<int>>>>>::operator [](std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&)'
with
[
_type=int
]
message : 引数リスト '(const std::map<std::string,std::vector<_type,std::allocator<int>>,std::lessstd::string,std::allocator<std::pair<const std::string,std::vector<_type,std::allocator<int>>>>>, const char [4])' を一致させようとしているとき
with
[
_type=int
]
error C2530: 'test1': 参照が初期化されずに宣言されています。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/09/13 07:10
退会済みユーザー
2023/09/13 08:29