samedi 20 janvier 2018

Howto drop a element from std::list?

Have a list ob objects and into std::for_each call each object, but need delete each object when finish the task for clear memory and call multiple objects in elapsed time, need remove and add items dinamicaly:

void Core::init()
{
    for(float i; i <= 2.0; i += 0.1)
    {
        Rectangle rectangle;
        rectangle.setLeft(-1.0 + i);
        rectangle.setTopToBottomInSeconds(1.0 + i);
        this->rectangles.push_back(rectangle);
    }
}

And in loop need remove each item:

void Core::draw()
{
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable( GL_BLEND );

    glClearColor(0.4, 0.4, 0.4, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    for (Rectangle &rectangle : this->rectangles)
    {
        // Draw object
        rectangle.draw();

        if(!rectangle.getIsVisible())
        {
            this->rectangles.erase(std::find(this->rectangles.begin(), this->rectangles.end(), rectangle));
        }

        // TODO: Add new rectangles here if is necessary.
    }
}

But the compiler show errors:

core/Core.cc:44:101: required from here /usr/include/c++/5/bits/predefined_ops.h:194:17: error: no match for ‘operator==’ (operand types are ‘Rectangle’ and ‘const Rectangle’)

I try change to const rectangle:

const Rectangle r;
r = rectangle;
this->rectangles.erase(std::find(this->rectangles.begin(), this->rectangles.end(), rectangle));

But:

core/Core.cc:44:93: required from here /usr/include/c++/5/bits/predefined_ops.h:194:17: error: no match for ‘operator==’ (operand types are ‘Rectangle’ and ‘const Rectangle’) { return *__it == _M_value; }

I using:

CCFLAGS += -lglut -lGL -Wall -Wextra -std=gnu++11

main:
    g++ \
    core/CoreLog.cc \
    core/physical_objects/Rectangle.cc \
    core/Core.cc \
    main.cc \
    $(CCFLAGS) \
    -o compiled/main

    chmod +x compiled/main

Aucun commentaire:

Enregistrer un commentaire