我有這個(簡化的)工作正常的代碼。在C++中丟棄限定符錯誤,並將地圖指針更改爲值
class T
{
map<string, int>* m;
public:
T()
{
m = new map<string, int>();
}
void get(const string& key, int& val) const
{
val = (*m)[key];
}
}
當我改變了指針變成價值,
class T
{
map<string, int> m;
public:
void get(const string& key, int& val) const
{
val = m[key];
}
};
我得到這個錯誤消息,有什麼不對?
In member function 'void T::get(const string&, int&) const':
constTest.cpp:12:20: error: passing 'const std::map<std::basic_string<char>, int>'
as 'this' argument of 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& ...'
discards qualifiers [-fpermissive]
val = m[key];
^