当我尝试编译以下代码时...
struct MemPages
{
size_t size;
volatile sig_atomic_t acc;
};
typedef std::map<unsigned long, MemPages> PagesMap;
PagesMap pagesMap;
............
pagesMap.insert(pair<unsigned long, MemPages>((unsigned long)addr, memPages ));
............
// This is Line 531
MemPages& mp = pagesMap[addr]; // Error here
我收到以下错误...
**replication.cpp:531: error: invalid conversion from ‘void*’ to ‘long unsigned int’
replication.cpp:531: error: initializing argument 1 of ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = long unsigned int, _Tp = MemPages, _Compare = std::less<long unsigned int>, _Alloc = std::allocator<std::pair<const long unsigned int, MemPages> >]’
make: *** [all] Error 1**
任何想法,这是怎么回事?
最佳答案
错误提示:
从“ void*
”到“ long unsigned int
”的无效转换addr
显然是void*
;地图的键类型是unsigned long
。您需要将unsigned long
(或者至少是可转换为整数的值)传递给operator[]
。
在代码中强制转换为整数((unsigned long)addr
)的指针很奇怪:确实没有任何理由这样做。如果std::map
的键类型应为指针类型,则应将其设为指针类型...
关于c++ - 使用std::map时出现此错误。为什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6821955/