Search code examples
c++arraysvectorerase

Vector array erase not working


My vector.erase() function gives me an error -> "error C2582: 'operator =' function is unavailable in 'Fire'

here is the code and the vector is declared in the header file.

any help is appreciated.

//header
std::vector<Fire> _fires;

//cpp
void Fire::update()
{
    for (unsigned int i = 0; i < _fires.size(); i++)
    {

    _fires[i].lifeTimer(); // updates the lifetime for all objects

        if (_fires[i]._lifetime >= 200)
        {
            _fires.erase(_fires.begin() + i); //erase not working
        }
    }
}

Solution

  • I think that your basic problem is that you have tried to design a single class which does two different things:

    • Contains a vector of fires
    • Implements the behaviour of a fire

    A much better design is to have two separate classes

    1 A class to implement the behaviour of a fire

    2 A class which contains a vector of fires, and implements methods, such as update, to iterate over the fires contained in the vector, invoking the fire methods.

    It would look like this:

    class Fire {
      // implements individual method on an instance of fire, such as  liefetime
    };
    
    class FireContainer {
      // implements method to deal with all the fires, such as update
      std:: vector< Fire > myFires;
    
    };
    

    However, if you want to continue with a single class, and have the vector of fires as global ( usually considered bad practice ) then the method update should not be a member of the Fire class, but a free function.