dimanche 4 octobre 2015

Error using const with [] operator [duplicate]

This question already has an answer here:

I have written a class:

class consttest
{
public:
  consttest()
  {
    test[1][1].insert(1);
    test[1][1].insert(2);
    test[1][1].insert(3);
  }

  bool get() const
  {
    auto it = test[1][1];
  }
  std::map<int, std::map <int, std::set<int>>>  test;

};


int _tmain(int argc, _TCHAR* argv[])
{
  consttest temp;
  temp.get();
}

When I compile this code, I get compilation error:

IntelliSense: no operator "[]" matches these operands
operand types are: const std::map<int, std::map<int, std::set<int, std::less<int>, std::allocator<int>>, std::less<int>, std::allocator<std::pair<const int, std::set<int, std::less<int>, std::allocator<int>>>>>, std::less<int>, std::allocator<std::pair<const int, std::map<int, std::set<int, std::less<int>, std::allocator<int>>, std::less<int>, std::allocator<std::pair<const int, std::set<int, std::less<int>, std::allocator<int>>>>>>>> [ int ]  c:\Work\Test_Projects\UniquePtr\UniquePtr.cpp   184

If I remove const from get(), then this error is gone. Can anybody help me understand why this error is coming?

Aucun commentaire:

Enregistrer un commentaire