C++ vector iterator not incremental - c++

I've been having a headache with this problem. I've challenged myself to make a CLI space shooter, where you have you spaceship, lasers and meteors to shoot. The problem is this: whenever a laser or a meteor reaches the boundaries of our arena, I want to erase from the vector so that it wouldn't cluster it up. Here's how my working code looks now:
std::vector<Meteoras>::iterator itMet = meteorai.begin();
std::vector<Lazeris>::iterator itLaz = lazeriai.begin();
while (itMet != meteorai.end() || itLaz != lazeriai.end())
{
if (itLaz != lazeriai.end())
{
if (itLaz->getCoord()->x == j && itLaz->getCoord()->y == i)
{
itLaz->move();
++j;
if (itLaz->getCoord()->x >= ILGIS - 1) continue;
else std::cout << itLaz->getIcon();
}
++itLaz;
}
if (itMet != meteorai.end())
{
if (itMet->getCoord()->x - 1 == j && itMet->getCoord()->y == i)
{
itMet->move();
++j;
if (itMet->getCoord()->x <= 0) continue;
else std::cout << itMet->getIcon();
}
++itMet;
}
}
So there are two "continues" in there. Instead of them I tried placing iterator removals (as in itLaz = lazeriai.erase(itLaz) but that the program seemed to crash during runtime giving the error that I stated before. I tried doing other logical checks but that didn't seem to work either. I would appreciate if someone could explain a proper way of removing a useless object (in this case a meteor/laser) from a vector.

The comment about erase() invalidating an iterator is true. The part about needing to restart at the beginning is wrong. (You could do, but you certainly don't need to.)
erase() returns a still-valid iterator which points to the element after the erased item.
if( test for erasing )
{
itLaz = lazeriai.erase(itLaz);
}
else
{
++itlaz;
}
You might be able to refactor your code to use std::remove_if() instead.

Related

VS2015 vector iterator not dereferencable

I have a little problem, I have to build a 2D game based on SDL.
I just keep getting this errror when I try to delete 2 objects that are colliding in "Scene".
std::vector<WaspObject*>::iterator itw;
std::vector<ProjectileObject*>::iterator itp;
for (itp = _projectiles.begin(); itp != _projectiles.end();)
{
for (itw = _wasps.begin(); itw != _wasps.end();)
{
if ((*itw)->Get_PositionY() + 37 >= (*itp)->Get_PositionY() + 0 && (*itp)->Get_PositionX()+0 >= (*itw)->Get_PositionX()+0 && (*itp)->Get_PositionX()+0 <= (*itw)->Get_PositionX()+49)
{
itw = _wasps.erase(itw);
itp = _projectiles.erase(itp);
}
else
{
itw++;
itp++;
}
}
}
_wasps and _projectiles are std vectors in this scene.
Can someone help me find the problem? Thanks in advance!
If you don't get any hits and increment itp in the inner loop, it might well run past _projectiles.end().
For example if there are more wasps than projectiles. Or if you are on the last projectile, where you will reach _projectiles.end() after a single itp++.
So you might want to test both end conditions at each turn of the inner loop.

Vector Collision

I am quite green regarding vectors, and this is my first time actually using them for collision checking. This is for my project, and I am stumped on how to implement the collision. The current Collision check and response codes I have seem to be ... bad design.
This is my code:
for(auto it = ArrayofEntities.begin(); it != ArrayofEntities.end(); it++)
{
CEntity * go = (*it);
for(auto i = ArrayofEntities.begin(); i != ArrayofEntities.end();)
{
//Collision for entities. Collision Event returns the iterator after an element is erased.
CEntity * other = (*i);
if (go != other)
{
if (!theCollision.CheckCollision(go, other, false, false, false, false)) //Checks if it has collided go with other
{
i = go->CollisionEvent(*other, ArrayofEntities); //Run collision code, setting i to the iterator which is returned.
//break;
}
else
{
i++;
}
}
else
{
i++;
}
}
}
CEntity is the base class for all the entities.
My CheckCollision just returns a true or false on the collision, and my collision event runs the collision and returns an iterator (because I might have to destroy things in the vector).
My collision event is below
vector<CEntity*>::iterator bullet::CollisionEvent(CEntity &other, vector<CEntity*> & theArray)
{
case ZOMBIE:
{
other.hp -= power * 0.01;//Effect
int Counter, index, bulletindex;
auto it = theArray.begin();
//Find the bullet and the other in the array.
for (it = theArray.begin(), Counter = 0; it != theArray.end();it++, Counter++)
{
CEntity *go = NULL;
go = (*it);
if (go == &other)
{
index = Counter;
}
if(go->ID == BULLET && go->GetX() == GetX() && go->GetY() == GetY())
{
bulletindex = Counter;
}
}
this->~bullet();//Delete the bullet
theArray.erase(theArray.begin() + bulletindex);
if(other.hp <= 0)
{
other.~CEntity();
it = theArray.erase(theArray.begin() + index); //delete from array.
return it;
}
it = theArray.begin() + index;
return it;
}
}
I have basically done this like how I would do an array. Just check it against itself. The error it gives is "Vector Iterator not Incrementable", on the first for loop after the collision event has been run.
So my question: 1) What am I doing wrong?
2) Is my thinking to do this like checking arrays wrong?
This is my school project, so I have full control of the codes.
I would prefer to have a quick fix over a complete rewriting of all the collision codes, but if it really comes down to it, I will rewrite my codes.
If you look at the implementation of std::remove_if, you'll see that they've solved the issue of iterator invalidation in another way. instead of erasing elements, they move them to the end of the array.
This may be the easiest solution for you as well. Keep an iterator which points after the last "live" entirty. It starts out at .end but as bullets hit things, you swap the entities to the back of your range and decrement that last-live iterator.
Then, when you're done looping over your array, you clean up with a single call to .erase.
And yes, you should use either std::unique_ptr<CEntity> or std::shared_ptr<CEntity> in the collection. In that way, .erase won't just erase the pointer but also the object pointed to.

Trouble removing elements from C++ vector

I'm trying to remove 'dead' bullets from my vector of bullets. Every frame, I'm calling the Bullet::update() function which looks like this:
void Bullet::update()
{
for(int i = 0; i != mAmmo.size(); i++)
{
if(mAmmo[i].sprite.getPosition().x > 700)
mAmmo[i].mAlive = false;
if(mAmmo[i].mAlive == false)
{
// I get a Debug Assertion Failed at runtime from this piece of code
mAmmo.erase(mAmmo.begin()+i);
}
if(mAmmo[i].mAlive == true)
{
mAmmo[i].sprite.move(mMovement);
}
}
}
Am I doing this completely incorrectly? This is the first time I've really used vectors more than just following through a tutorial. If I need to post any more code, just tell me. I've been working on this for the past few hours, so I'm a wee bit desperate to get this to work.
Thanks in advance!
You're easily walking into undefined behavior as soon as the ith element is the last element in your list. Use iterators, and pay special attention to the return value of erase(), as it automatically advances the iterator for you so your loop doesn't have to.
void Bullet::update()
{
for (auto it = mAmmo.begin(); it != mAmmo.end();)
{
if(it->sprite.getPosition().x > 700)
it->mAlive = false;
if (!it->mAlive)
{
// erase and get next iterator
it = mAmmo.erase(it);
}
else
{ // move and increment
it->sprite.move(mMovement);
++it;
}
}
}

Vector int iterators

I'm currently trying to run through a vector of ints with an iterator. Every time I run through it, I'm only getting a value of 0. Both vectors do have valid data in them.
OffenseName and Down are private member data that the user inputs.
vector <string> :: iterator itr;
vector <int> :: iterator itrdown;
int count = 0;
for (itr = dataOffenseName.begin(); itr!= dataOffenseName.end(); ++itr)
{
if ( OffenseName == *itr )
{
for (itrdown = dataDown.begin(); itrdown != dataDown.end(); ++itrdown)
{
//Here itrdown is always coming at 0. The dataDown vector
//does have valid data in it
if (Down == *itrdown)
{
count++;
}
else
{
break;
}
}
}
}
return count;
if (Down = *itrdown)
Come on, the oldest trick in the book :)
if (Down == *itrdown)
will be correct.
if (Down = *itrdown)
should be
if (Down == *itrdown)
The former is not a comparison, just an assignment, meanwhile you meant second: comparison.
However, there is a more important lesson in here: turn the warnings on when building. If you had done, you could not have missed this one unless you disregard warnings without evaluating them which is not a good idea.
That is probably a more important lesson to learn out of this situation because you would not need to debug issues like this at all then. ;-)

Valgrind detects invalid read error in simple Iterator class

Valgrind detects an invalid read error I don't know how to fix or to be more precise: I don't know what the problem is.
Invalid read of size 8
at 0x443212: std::vector<Tile*, std::allocator<Tile*> >::end() const
by 0x44296C: Collection<Tile*>::Iterator::operator++()
The Iterator class is very simple (and actually a somewhat bad piece of programming) but sufficient for my needs right now. I think there are three methods you should know to hopefully help find my problem:
Iterator(size_t x, size_t y, const TileCollection& tiles)
: mTiles(&tiles)
, mX(mTiles->begin())
, mY(mTiles->at(x).begin())
{
std::advance(mX, x);
std::advance(mY, y);
bool foundFirst = false;
while (!foundFirst)
{
while (mY != mX->end() && *mY == 0) ++mY;
if (mY != mX->end()) foundFirst = true;
else
{
++mX;
if (mX != mTiles->end()) mY = mX->begin();
}
}
}
Iterator Iterator::operator++()
{
bool foundNext = false;
++mY;
while (!foundNext)
{
while (mY != mX->end() && *mY == 0) ++mY;
if (mY != mX->end()) foundNext = true;
else
{
++mX;
if (mX != mTiles->end()) mY = mX->begin();
}
}
return *this;
}
void TileCollection::add(Tile* tile)
{
Point2D p(tile->getPosition());
std::vector<Tile*> tmp(1, (Tile*)0);
if ((size_t)p.x >= mTiles.size())
mTiles.resize(p.x + 1, tmp);
if ((size_t)p.y >= mTiles.at(p.x).size())
mTiles.at(p.x).resize(p.y + 1, (Tile*)0);
mTiles.at(p.x).at(p.y) = tile;
++mNumTiles;
}
The actual code that is causing the valgrind error is the line:
while (mY != mX->end() && *mY == 0) ++mY;
...of the Iterator::operator++ method.
It looks to me that, at the least, the following line in operator++
if (mX != mTiles->end()) mY = mX->begin();
is lacking a suitable else-clause.
Consider what happens when mX actually reaches mTiles->end(): You will enter a new iteration of the outer while loop; the first line in that loop (the line that causes the Valgrind error) will evaluate mX->end() and thus attempt to dereference mX -- but mX is mTiles->end(), and it's not correct to dereference the end iterator of a collection since it doesn't actually reference an element of the collection. It looks to me as if this may be the cause of your Valgrind error.
(Note that the constructor contains essentially the same code.)
More generally, I think you need to think about how you handle reaching the end of your two-dimensional array. How does the client of your Iterator check whether it has reached the end of the iteration? How do you expect your operator++ to handle the case when it reaches the end of the two-dimensional array? Should it protect itself against getting called too often?
You can try to split up the statement in order get find out where the error occurs:
while (mY != mX->end()) // maybe here
{
if (*mY != 0) // maybe here
{
break;
}
++mY; // maybe here
}
Compiling with GCC compiler option -fno-inline helps to get a nicer stack-trace, which can help you to trace the error. It will also make your program very slow, so don't forget to remove it later.