我有以下代码 -
#include <map>
using Type1 = std::map<std::string, unsigned long long>;
using Type2 = std::map<std::string, Type1>;
class T
{
private:
Type2 mymap;
public:
const Type1& get(const std::string& key) const;
};
const Type1& T::get(const std::string& key) const
{
return mymap[key];
}
int main(void)
{
}
这不编译,编译器抱怨 -
maps.cpp: In member function ‘const Type1& T::get(const string&) const’: maps.cpp:17:20: error: passing ‘const Type2 {aka const std::map<std::basic_string<char>, std::map<std::basic_string<char>, long long unsigned int> >}’ as ‘this’ argument of ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::basic_string<char>; _Tp = std::map<std::basic_string<char>, long long unsigned int>; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::map<std::basic_string<char>, long long unsigned int> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::map<std::basic_string<char>, long long unsigned int>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::basic_string<char>]’ discards qualifiers [-fpermissive] return mymap[key]; ^
我需要帮助理解这个错误。据我所知,我不是在get函数中修改“this”。
谢谢!