Why is this vector iterator not incrementable?

前端 未结 8 1256
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 10:41

I\'m trying to delete the vector\'s content and I\'m getting an error - vector iterator is not incrementable, why is that?

This is my destructor:

C         


        
8条回答
  •  孤城傲影
    2020-12-13 10:58

    Posting this just incase anyone else has this this problem and attempts this solution wondering why it's not working here's an actual solution/explanation.

    @Steve Jessop - Your code is flawed and you've also got it written here... ( I've also edited his post to fix the issue as soon as it's approved it'll be fixed in the original post )

    http://techsoftcomputing.com/faq/3779252.html

    I don't see how this is a "Solution" to the issue when it create an new issue by making an endless loop there should be a deleteIterator++ within the while loop so that it actually reaches the end of the vector.

    Also I've ran into this problem and my solution was inside the while loop checking whether the iterator was equal to the end or if the vector size was 0 and breaking before attempting to incrementing the iterator.

    Ex.

        std::vector::iterator Rank_IT = CurrentPlayers.begin();
    
        while ( Rank_IT != CurrentPlayers.end() ) 
        {    
            RankPlayer* SelPlayer = (*Rank_IT);
    
            if( strstr( SelPlayer->GamerTag, this->GamerTag ) != NULL )
            {
    
                delete[] SelPlayer->PlayerData;
                delete[] SelPlayer;
                Rank_IT = CurrentPlayers.erase( Rank_IT );
            }
    
            if( Rank_IT == CurrentPlayers.end() || CurrentPlayers.size() == 0 )
            {
                break;
            }
                ++Rank_IT;
        }
    

提交回复
热议问题