c++ vectors causing break in my game [closed]

Well I just had a quick look at that monstrosity, and I’m not going to look too far into it but right off the bat I see a problem.

I don’t know if this is related to this particular problem you are asking about, but this type of thing isn’t going to work the way you think:

for(long index=0; index < (long)RegularShots.vshots.size(); ++index) 
{ 
   RegularShots.vshots[index].y-=2;
   // Delete Shots
   if(RegularShots.vshots[index].y<-16)
   {
     RegularShots.vshots.erase(RegularShots.vshots.begin()+index);
   }
}

When you erase an item from the vector, the next item in the vector is moved down one, so every time you erase an item, you are skipping over the next item and not performing your test on it.

A quick fix here would be to do an index– after the erase call.

But based on what I’ve seen, I would seriously recommend that you do some reading on STL containers and iterators in particular. And then read some books on good coding style 😉

Browse More Popular Posts

Leave a Comment