简体   繁体   中英

C++ vector erase at specific index doesn't get it to work

My problem is that when I try to erase a secefic object in my vector it removes all the object before that object.

I've done this in the Bullet class, can it have something to do with it?:

Bullet operator=(Bullet);

Bullet Bullet::operator=(Bullet b) {
    Bullet newBullet(NULL, NULL);
    return(newBullet);
}

If I don't have this I get

error C2582: 'operator =' function is unavailable in 'Bullet'

This is where I define each element to the bullets vector:

if (p_shoot) {
    bullets.insert(bullets.begin(), Bullet(p_getHitboxX(), p_getHitboxY() + (p_getHitboxH() / 2)));
    p_shoot = false;
}

This is the code where it happens:

if (!bullets.empty()) {
    for (unsigned int i = 0;i < bullets.size();) {
        if (bullets.at(i).bullet_collisionCheck(rect)) {
            bullets.erase(bullets.begin() + i);
        }
        else i++;
    }
}

I don't get it why it removes all the Bullet objects before the specific Bullet object in the index I want to erase. It's like it removes all objects between bullets.begin() and i.

Let's say that bullets contains 3 Bullet objects, the first bullet collides making bullet_collisionCheck return true, which means that the index is 0 but then it erases bullets[2] first then bullets[1] and lastly bullets[0] the one i wanted to erase first.

I've been experimenting a bit to see what's going on so I wrote this code to see exactly what happens:

bullets.push_back(Bullet(100, 100));
bullets.push_back(Bullet(200, 200));
bullets.push_back(Bullet(300, 300));
bullets.push_back(Bullet(400, 400));
bullets.push_back(Bullet(500, 500));
bullets.erase(bullets.begin() + 3);

When this code runs, it should remove the Bullet(400, 400) but it removes the last, Bullet(500, 500). I changed the value to 1 which means it should remove Bullet(200, 200) but it still removes the last, Bullet(500, 500). What is going on?

Please help.

I found the solution to the problem, thank you all for your help.

The problem was:

Bullet operator=(Bullet);

Bullet Bullet::operator=(Bullet b) {
    Bullet newBullet(NULL, NULL);
    return(newBullet);
}

I just had to change it to :

void operator=(const Bullet &b);

void Bullet::operator=(const Bullet &b) {
    bullet_hitbox.x = b.bullet_hitbox.x;
    bullet_hitbox.y = b.bullet_hitbox.y;
}

Give this a try: Just using iterating based on iterators and not on indexing.

if(!bullets.empty()) 
    {
        for ( auto it = bullets.begin(); it != bullets.end(); ++it ) 
        {
            if ( it->bullet_collisionCheck(rect) ) 
            {
                it = bullets.erase( it );
                --it;
            }
        }
    }

使用“ bullets.insert(bullets.begin(),...”)添加新对象将在开始时添加新对象,因此,第一个创建的使bullet_collisionCheck返回true的项目符号将是最后一个(bullet [2])。 if (bullets.at(i).bullet_collisionCheck(rect))引起问题的条件,因为bullets.erase(bullets.begin() + i);应该可以正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM