Map in Map Map.Clear() error
Map in Map Map.Clear() error
void destroy()
{
AList::const_iterator a;
for(a = AList.begin(); a != AList.end();)
{
if(!a->second.BList.empty())
a->second.BList.clear();//will give error if not mutable
}
}
typedef std::map<unsigned int,int> bmap;
typedef std::map<unsigned int,someStruct> Alist;
typedef struct someStruct
{
float x,y,z;
bmap BList; //needs to be mutable for Blist.clear() above.
//mutable bmap BList; //<---like this
} someStruct;
我只是在一个类似但不相同的问题中改变了可变选项。我的问题是,我做的是对的吗,或者这样做是否有陷阱?提前感谢您的帮助。
//error given: (if otherwise not mutable)
// error: passing 'const AList' as 'this' argument of 'void std::map<_Key, _Tp, _Compare, _Alloc>::clear() [with _Key = unsigned int, _Tp = int, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, int> >]' discards qualifiers
您应该使用iterator
而不是const_iterator
,如果您的意图是调用clear
。const_iterator
用于只调用const
成员函数的情况。
使用mutable
不适合这种情况。如果成员变量不是对象可见状态的一部分,例如缓存的数据,则只标记mutable
。
你试过放简单的iterator
吗?
AList::iterator a;
const_iterator
不允许修改成员(有点像正常上下文中的const
)。
您给出的代码不正确。destroy
应该是类的const成员,但您将其显示为全局函数。因为/If destroy
是const方法,clear
不能在它上面工作
相关文章:
- "error: no matching function for call to"构造函数错误
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 使用一个考虑到std::map中键值的滚动或换行的键
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 允许从 std::map 的密钥窃取资源?
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将重物插入std::map
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- boost::spirit::qi::phrase_parser() into std::map error
- std::map linker error ndk r8c with APP_STL := gnustl_static
- Map in Map Map.Clear() error