C++ Map Access Discards Qualifiers (Const)

C++ map access discards qualifiers (const)

std::map's operator [] is not declared as const, and cannot be due to its behavior:

T& operator[] (const Key& key)

Returns a reference to the value that is mapped to a key equivalent to key, performing insertion if such key does not already exist.

As a result, your function cannot be declared const, and use the map's operator[].

std::map's find() function allows you to look up a key without modifying the map.

find() returns an iterator, or const_iterator to an std::pair containing both the key (.first) and the value (.second).

In C++11, you could also use at() for std::map. If element doesn't exist the function throws a std::out_of_range exception, in contrast to operator [].

error: passing ‘const std::mapint, int’ as ‘this’ argument discards qualifiers [-fpermissive]

operator[] doesn't have a const qualifier in std::map, as you can see from the documentation, e.g. std::map::operator[] - cppreference.com:

Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.

Therefore you cannot use it directly on a const instance. Use at instead (ref std::map::at - cppreference.com) if you can afford C++11 features.

Declarations for those member functions follow:

T& operator[](const key_type& x);
T& operator[](key_type&& x);
T& at(const key_type& x);
const T& at(const key_type& x) const;

Discards qualifiers Error accessing a vector of a map in a class?

In:

int size = c.cmap[*(strlist.begin() + j)].size();

you should write:

int size = c.cmap.at(*(strlist.begin() + j)).size();

instead.

You are passing c as const C1&, which means that c.cmap is also const.

std::map has no const overload for operator[], because operator[] performs an insertion if the key does not exist. This is in order to make statements like:

std::map<std::string, int> map;
map["test"] = 1;

work.

Const map element access

at() is a new method for std::map in C++11.

Rather than insert a new default constructed element as operator[] does if an element with the given key does not exist, it throws a std::out_of_range exception. (This is similar to the behaviour of at() for deque and vector.)

Because of this behaviour it makes sense for there to be a const overload of at(), unlike operator[] which always has the potential to change the map.

When using map::erase() error: passing 'const ***' as 'this' argument of '***' discards qualifiers [-fpermissive] reported

void Gestion::EliminateObject(string nomobjetg) const

You are trying to modify objectname with your call to objectname.erase(it), and a const function will not let you do that, unless objectname is mutable.

Remove const:

void Gestion::EliminateObject(string nomobjetg)

or

Make objectname mutable

mutable Objectmap objectname;

error: passing ‘const std::unordered_mapchar, int’ as ‘this’ argument discards qualifiers

You're doing wrong in the first case. You should use [&cnt_w = cnt_w] or just [&cnt_w]. cnt_w here is non-const.

What you're doing is getting the address of cnt_w



Related Topics



Leave a reply



Submit