我在调用时遇到问题:
m_baseMap.find(baseName)->second->AddVehicale(vehicaleToAdd);
m_baseMap
下有一条红线,错误是:“对象具有与成员函数不兼容的类型限定符”。 basemap 定义如下:
map <string, const Base*> m_baseMap;
我该如何解决?
最佳答案
问题不在于 find()
但是调用 AddVehicale 因为 map 指定了 const Base*
.您要么需要制作 map
是map<string, Base *>
或者确保 AddVehicale
是一个 const 方法(这意味着您 promise 不会修改 map 中指向的对象)例如void Base::AddVehicale(Vehicale &v) const;
据我所知,编译器会根据当时映射本身是否为常量来选择是使用常量查找还是非常量查找(比如如果您对映射有常量引用)
附言Vehicale 拼写为 Vehicle(我用 google 拼写检查,如果我不确定,搜索这个词,它会建议正确的拼写)
关于c++ - 如何在 map 中使用 const?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3722971/