2013-04-02 108 views
0

當我嘗試運行我的程序時,出現'map/set iterator not incrementable'錯誤。我讀過它會在你使迭代器無效時發生,但我只編輯由迭代器指向的對象。這是正常的行爲嗎?我能做些什麼呢? 如果我刪除這一行那麼有沒有錯誤:映射迭代器不可增量

iterator->second.neighbour[direction] = &(find_iter->second); 

全碼:

void Gomoku::addUniqueMap(const map_Map& map_to_add) 
{ 
    this->unique_maps.push_front(map_to_add); 

    std::list<map_Map>::iterator map_iter = this->unique_maps.begin(); 
    map_Iterator iterator = map_iter->begin(); 

    for(; iterator != map_iter->end(); ++iterator) 
    { 
     for(int i = 1, direction = 0; i > -2; --i) 
     { 
      for(int j = -1; j < 2; ++j) 
      { 
       if(iterator->second.neighbour[direction] == nullptr) 
       { 
        ++direction; 
        continue; 
       } 
       if(i == 0 && j == 0) 
        continue; 

       COORD pos(iterator->first.x + j, iterator->first.y + i); 
       wrap(pos, this->map_size); 

       map_Iterator find_iter = map_iter->find(pos); 
       if(find_iter != map_iter->end()) 
       { 
        iterator->second.neighbour[direction] = &(find_iter->second); 
       } 

       ++direction; 
      } 
     } 
    } 
} 

'map_Map' - std::map<COORD, Pawn>
'map_Iterator' - std::map<COORD, Pawn>::iterator
'典當' -

struct Pawn 
{ 
    Pawn(Player player); 
    Player  player; 
    const Pawn* neighbour[8]; 
}; 
+0

你可以發佈'unique_maps'的類型嗎?你也應該嘗試刪除'auto',代碼會更清晰(即使有點冗長)(請使用typedefs)。也許編譯器爲第二個'auto'推導出的類型並不是我們所期望的,而且你正在訪問'end()'迭代器......而且,如果你的映射的大小是1x1,你將有'find_iter' =='iterator'。 – Synxis

+0

@Synxis我用類型替換了汽車,但它沒有幫助。 我從不訪問'end()'迭代器,如果迭代器不是end(),我總是先檢查它。 –

+0

永遠不要明確地設置一個迭代器到'end()'並不意味着這個迭代器不能取得'end()'的值...... – Synxis

回答

0

錯誤的原因是我是acce因爲我正在修改out數組範圍的元素,可能它已經爲迭代器做了些什麼。
在調試版本中,我得到了map/set iterator not incrementable錯誤,而在發佈版本迭代器不能增加,它導致程序無限循環。