マップの指定されたキーと値のペアを繰り返し処理し、関数のパラメータで指定されたキーが存在するかどうかを確認するテンプレート関数を作成しようとしています。イテレータが定義されていないという奇妙なコンパイラエラー
コード
template < class Key, class Value >
bool CheckMapForExistingEntry(const std::map< Key, Value >& map, const std::string& key)
{
std::map< Key, Value >::iterator it = map.lower_bound(key);
bool keyExists = (it != map.end && !(map.key_comp() (key, it->first)));
if (keyExists)
{
return true;
}
return false;
}
しかし、何らかの理由で、私は私のコードはコンパイルされません理由を把握することができないように次のように
実装が見えます。
error: expected ';' before 'it'
error: 'it' was not declared in this scope
これまで私が遭遇したことがありますが、これは通常、私が間違いを犯したために見つけやすいものです。ここで何が起こっているのでしょうか?
template < class Key, class Value >
bool CheckMapForExistingEntry(const std::map< Key, Value >& map, const std::string& key)
{
typename std::map< Key, Value >::iterator it = map.lower_bound(key);
bool keyExists = (it != map.end && !(map.key_comp() (key, it->first)));
if (keyExists)
{
return true;
}
return false;
}
This articleがかなり詳細に説明します。
[テンプレートとタイプ名]キーワードをどこで、どこに入れなければならないのですか?(http://stackoverflow.com/questions/610245/where-and-why-do-i-haveテンプレート名と型名のキーワード) – Lambdageek