質問

こんにちはすべて私はメンバー関数に次のことを持っています

int tt = 6; 
vector<set<int>>& temp = m_egressCandidatesByDestAndOtMode[tt]; 
set<int>& egressCandidateStops = temp.at(dest);

メンバー変数の次の宣言

map<int, vector<set<int>>> m_egressCandidatesByDestAndOtMode;

ただし、コンパイル時にエラーが発生します(Intelコンパイラ11.0)

1>C:\projects\svn\bdk\Source\ZenithAssignment\src\Iteration\PtBranchAndBoundIterationOriginRunner.cpp(85): error: no operator "[]" matches these operands
1>            operand types are: const std::map<int, std::vector<std::set<int, std::less<int>, std::allocator<int>>, std::allocator<std::set<int, std::less<int>, std::allocator<int>>>>, std::less<int>, std::allocator<std::pair<const int, std::vector<std::set<int, std::less<int>, std::allocator<int>>, std::allocator<std::set<int, std::less<int>, std::allocator<int>>>>>>> [ const int ]
1>          vector<set<int>>& temp = m_egressCandidatesByDestAndOtMode[tt]; 
1>                                                                    ^

私はそれが愚かな何かでなければならないことを知っていますが、私が間違ったことをしたことを見ることができません。

アップデート 私はこれをconstメンバー関数から呼んでいるため、メンバー変数のタイプがconstであるため、次のようなものが修正する必要があると思いました。

int dest = 0, tt = 6; 
const set<int>& egressCandidateStops = m_egressCandidatesByDestAndOtMode[tt].at(dest); 

しかし、サイコロはありません...それでも同じエラー。

役に立ちましたか?

解決

オペランドタイプは次のとおりです。 const std :: map<int…

map::operator[] で動作しません const map.

答えた これは数日前です。

Map :: operator []は少し奇妙です。これを行います:

  1. キーを探してください。
  2. 見つかった場合は、返品してください。
  3. そうでない場合は、それを挿入し、デフォルトの関連する値を構築します。
  4. 次に、新しい値への参照を返します。

ステップ3はconstnessと互換性がありません。 2つの異なる機能するオペレーター[]のオーバーロードがあるのではなく、言語はMAP :: constオブジェクトの検索を使用することを強制します。

他のヒント

のプロトタイプ []

 data_type& operator[](const key_type& k)

IE非const操作であるため、constメンバー関数のメンバーでそれを呼び出すことはできません。

コードを次のように変更できます。

std::map<...>::const_iterator where = m_egressCandidatesByDestAndOtMode.find(tt);
if (egressCandidatesByDestAndOtMode.end() != where) {
    const vector<set<int>>& temp = where->second;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top