C++ "Error: Passing 'Const Std::Map<Int, Std::Basic_String<Char> >' as 'This' Argument of ..."

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

operator[] hasn't 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;

C++ error: passing 'const std::mapint, std::basic_stringchar ' as 'this' argument of ...

string color::getColorText() const {
return colors[cColortype];
}

The issue is that you've marked the function as const. The operator[] on std::map is marked as non-const, and cannot be used in a const function like this. You need to manually use std::map::find (or other mechanism) to search for the input type and handle the case where it's not found.

If you're using C++11, you can instead use std::map::at, which IS allowed to be used on a constant map, and throws an exception if the requested element is not present.

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

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;

C++ error: passing ‘const umap_int {aka const std::unordered_mapint, int}’ as ‘this’ argument discards qualifiers [-fpermissive]

Inside const method you can only call for M its const member functions. Both unordered_map::operator[] overloads are non-const - reference. So you cannot use it inside const get_M. You could remove const qualifier from get_M signature, or use find which has const overload but then you need to handle a case when mapped value doesn't exist for the passed key:

const umap_int::mapped_type & get_M(int key) const {
//return M[key];
auto it = M.find(key);
if (it != M.end())
return it->second;
// do sth here ...
// throw exception
// make static variable with default value which will be accessed
}

error: passing ‘const std::__cxx11::list’ as ‘this’ argument discards qualifiers

const std::list<Student>& GetStudents() const;

and

this->GetStudents().push_back(t);

are in conflict. You have a contract to not modify the list you get from GetStudents(), but you try to do that with push_back.

Solution:

void StudentRegistry::Add(const Student& t) {
students.push_back(t);
}

passing const ... as this argument discards qualifiers [-fpermissive]

This

Complex operator - (Complex const &obj) { ...

and others are non-const member functions. Member functions are non-const by default, meaning they are declared to modify this. You cannot call them on a const instance. Most of your operators do not modify this, hence should be declared as const:

Complex operator - (Complex const &obj) const { ...
// ^^


Related Topics



Leave a reply



Submit