pighead10
pighead10

Reputation: 4305

'vector iterators incompatible'

std::vector<Enemy*>::iterator it;
for(it;it!=tracked.end();it++){
    (*it)->update(timeSinceLastFrame);
}

tracked is the vector:

std::vector<Enemy*>

Why am I getting this error? (please say if I haven't included enough details)

Upvotes: 3

Views: 250

Answers (2)

Etienne de Martel
Etienne de Martel

Reputation: 36852

You never initialized the iterator.

for(std::vector<Enemy*>::iterator it = tracked.begin();it!=tracked.end();it++){
    (*it)->update(timeSinceLastFrame);
}

Many implementations (such as VC++, which you appear to be using) perform checks in debug to make sure that when two iterators are compared, they belong to the same object. A default constructed iterator does not belong to any particular instance, and as such the it != tracked.end() check will fail with that error.

Upvotes: 12

kol
kol

Reputation: 28688

You haven't initialized it. Try this:

std::vector<Enemy*>::iterator it;
for(it=tracked.begin();it!=tracked.end();it++){
    (*it)->update(timeSinceLastFrame);
}

Upvotes: 2

Related Questions