在我的頭文件中,我包含了std :: map並使用了適當的名稱空間。
我的一個成員是:使用映射迭代器編譯錯誤
map<unsigned int, double> pT_Spam;
在我.cpp文件我試圖做一些事情,我一直在做頻繁的一段時間:
for(map<unsigned int, double>::iterator it=pT_Spam.begin() ; it!=pT_Spam.end() ; it++) {/*code*/}
以上,即使是中提到的一個在cplusplus.com上使用std :: map的例子。儘管我已經做了相當多的代碼的其他部分造成任何編譯錯誤一樣,在這一行,我從Cygwin的出現以下錯誤:
error: conversion from `std::_Rb_tree_const_iterator<std::pair<const unsigned int, double> >' to non-scalar type `std::_Rb_tree_iterator<std::pair<const unsigned int, double> >' requested
這似乎很奇怪。任何想法可能是錯誤的? (我的頭,當然,包括在我的.cpp)
這是抱怨,由begin()返回的迭代器是一個const_iterator,但你將它分配給一個迭代器。但我不知道爲什麼,這對我來說也是正確的。這是什麼編譯器? – Joe 2012-01-06 01:53:09
[begin()](http://cplusplus.com/reference/stl/map/begin/)似乎也能夠返回非常量迭代器。 我在Windows上使用Cygwin的g ++。 – jathanasiou 2012-01-06 01:58:00