2013-06-27 115 views
3

我正在以下調試錯誤:通過的所有元素C++調試斷言失敗:地圖/套迭代器不dereferencable

for(std::map<int,IO::ctcolor>::iterator it = IO::colorTable.begin(); it != IO::colorTable.end();) { 
    //left out some code here, but even without it doesn't work 
    cout << "dummy"; //gets printed multiple times while iterating, so the map is not empty 
    ++it; 
} 

的for循環遍歷:

Debug Assertion Failed! 
Program: Path\To\vc\include\xtree 
Line: 237 

Expression: map/set iterator not dereferencable 

蒙山這行代碼IO::colorTable但在達到最後時不會停止,我可以在將數據打印到循環內的控制檯時看到此情況。

編輯:我只注意到後的for循環,我錯誤地試圖訪問it

+0

你是否通過調試器運行它? – Huy

+0

我注意到你在循環結尾放了一個'++ it'。你確定你還沒有在你的真實代碼中增加for(;;)語句的增量部分? – dhavenith

+0

@Huytard visual Studio 2012 – user1950929

回答

3

我相信問題是,你增加價值沿途某處沒有注意到該行發生錯誤,該錯誤意味着你正在試圖解引用end()迭代器。

看看這個code我相信它和你的很相似,因爲你可以看到它工作正常。

#include <map> 
#include <iostream> 
using::std::map; 
using::std::pair; 

class IO 
{ 
private: 

class ctcolor 
{ 
private: 
    char c; 
public: 
    ctcolor(char c){c='c';} 
}; 

map<int,ctcolor> colorTable; 
public: 
IO(){ 
    for(int i=0;i<10;i++) 
{ 
    colorTable.insert(pair<int,IO::ctcolor>(i,'c')); 
} 
} 
void io(){ 
    for(std::map<int,IO::ctcolor>::iterator it = IO::colorTable.begin(); it != IO::colorTable.end();) { 
std::cout << "dummy"; 
++it;} 
} 

}; 

int main() 
{ 
IO io; 
io.io(); 
return 0; 
} 
+0

我在for循環後面的行中發現了問題 – user1950929

+0

很好的代碼在bro上; – petric