我有这个(简化的)代码可以正常工作。
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];
^