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
'Pawn' -

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

1 回答 1

0

错误的原因是我正在访问和修改neighbour[8],所以我正在修改数组范围外的元素,并且可能它对迭代器做了一些事情。
在 Debug 版本中,我map/set iterator not incrementable在 Release 版本中遇到错误,迭代器无法递增,导致程序无限循环。

于 2013-04-03T19:12:50.287 回答