小编Mer*_* Xu的帖子

c ++ STL map.find()或map.operator []不能在具有const限定符的类成员函数中使用

我对以下代码感到困惑,为什么它无法成功编译?

class Test { 
public:
  int GetValue( int key ) const
  {
      return testMap[key];
  }

  map<const int, const int> testMap; 
};
Run Code Online (Sandbox Code Playgroud)

始终存在编译错误:

error C2678: binary '[': no ??operator found which takes "const std :: map <_Kty,_Ty>" type of the left operand operator (or there is no acceptable conversion).
Run Code Online (Sandbox Code Playgroud)

我试图将const限定符放在任何地方,但它仍然无法通过.你能告诉我为什么吗?

c++ stl const map member

3
推荐指数
1
解决办法
856
查看次数

标签 统计

c++ ×1

const ×1

map ×1

member ×1

stl ×1