Vector unincrementable.

I think this has something to do with a problem I had before with pointers but I can't figure it out.

On Space:
1
2
3
4
5
6
7
8
9
10
11
void entity_Bizzle::onKeySpace()
  {
    if(!attacking)
    {
      attacking=true;   
      attack_timer = attack_speed;
    
      base_entity* weapon = new entity_activeWeapon;
      room->createEntity(weapon);
    }
  }


1
2
3
4
  virtual void createEntity(base_entity*& new_entity)
  {
    entities.push_back(new_entity);
  }


Whenever I hit space instead of making a weapon object it just errors out with:
Expression: vector iterator not incrementable.

Not sure why it's doing that, any help is appreciated.
Thanks,
Tyler
the std::vector::iterator is not incrementable. In order to access an item you must use the [] operator.

i'm guessing that entities is a std::vector<base_entity*> pointers? Instead of passing the function a reference to a pointer, try just passing the pointer.

1
2
3
4
virtual void createEntity(base_entity* new_entity)
  {
    entities.push_back(new_entity);
  }

Oh I left the & in while trying to fix it. It doesn't work still if you take it out.

1
2
3
4
5
  virtual void onKeySpace()
  {
  for(e_it=entities.begin(); e_it<entities.end(); e_it++)
    (*e_it)->onKeySpace();
  }


Here's where it cycles through if that helps any.
I'm gonna guess you're storing an iterator somewhere and the push_back invalidates the iterator.

I'm also puzzled at the parameter type for createEntity. A simple pointer would make more sense, although it wouldn't be the cause of your problem.
1
2
3
4
  virtual void createEntity(base_entity* new_entity)
  {
    entities.push_back(new_entity);
  }


It's actually supposed to look like this, I never changed it back when I was trying to fix it. Either way it doesn't work.
does your compiler allow breakpoints?

1
2
3
4
5
virtual void onKeySpace()
  {
  for(e_it=entities.begin(); e_it<entities.end(); e_it++)
    (*e_it)->onKeySpace();
  }


seems like an infinite loop.
I use VS C++.

Also an infinite loop wouldn't throw an error.
1
2
3
4
5
  virtual void onKeySpace()
  {
  for(e_it=entities.begin(); e_it<entities.end(); e_it++)
    (*e_it)->onKeySpace();
  }



As I said earlier, your push_back invalidates a stored iterator. This iterator, in fact. Use an index instead:

1
2
3
4
5
virtual void onKeySpace()
{
    for ( unsigned i=0; i< entities.size(); ++i)
        entities[i]->onKeySpace() ;   
}

Topic archived. No new replies allowed.