Removing Sprites from vector in sfml 2.1

2019-05-30 07:36发布

问题:

I'm trying to remove sprites from a vector, but to no avail and I don't know what's wrong(VS 2010 with SFML 2.1). It simply doesn't compile. Any tips appreciated.

Its this line, that seems problematic:

SpriteVector.erase(std::remove(SpriteVector.begin(), SpriteVector.end(), SpriteIt), SpriteVector.end());

Full code:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    sf::RenderWindow mMainWindow(sf::VideoMode(1200, 900), "Map");
    mMainWindow.setFramerateLimit(60);

    sf::RectangleShape rect;
    rect.setSize(sf::Vector2f(1500, 500));
    sf::Texture unittexture;
    unittexture.loadFromFile("sprite.png");

    std::vector<sf::Sprite> SpriteVector;
    std::vector<sf::Sprite>::iterator SpriteIt;

    while (mMainWindow.isOpen())
    {
        sf::Event event;
        while (mMainWindow.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                mMainWindow.close();
            }
            else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::A)   
            {
                sf::Sprite newSprite;
                newSprite.setTexture(unittexture);
                newSprite.setPosition(mMainWindow.mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(mMainWindow).x-50,sf::Mouse::getPosition(mMainWindow).y-50))); 
                SpriteVector.push_back(newSprite);
            }
            else if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Right)
                    {
                    //  sf::Vector2f mousecoords(mMainWindow.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y)));     
                    //  SpriteVector.remove_if([=](sf::Sprite newSprite){return newSprite.getGlobalBounds().contains(mousecoords); });     //example code that would work with a list

                        SpriteVector.erase(std::remove(SpriteVector.begin(), SpriteVector.end(), SpriteIt), SpriteVector.end());           // This doesn't work and I don't know why.
                    }
        }   
    mMainWindow.clear();
    mMainWindow.draw(rect);

        for(auto &SpriteIt = SpriteVector.begin();SpriteIt != SpriteVector.end();++SpriteIt)
        {
            mMainWindow.draw(*SpriteIt);
        }
        mMainWindow.display();


    }

    return 0;
}

Error log:

bei Anpassung der Argumentliste '(sf::Sprite, const std::_Vector_iterator<_Myvec>)'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<sf::Sprite,std::allocator<sf::Sprite>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(1829): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "_FwdIt std::_Remove<sf::Sprite*,_Ty>(_FwdIt,_FwdIt,const _Ty &)".
1>          with
1>          [
1>              _FwdIt=sf::Sprite *,
1>              _Ty=std::_Vector_iterator<std::_Vector_val<sf::Sprite,std::allocator<sf::Sprite>>>
1>          ]
1>          c:\users\bogdan\documents\visual studio 2010\projects\spielkarte bev\sfmltest\main.cpp(40): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "_FwdIt std::remove<std::_Vector_iterator<_Myvec>,std::_Vector_iterator<_Myvec>>(_FwdIt,_FwdIt,const _Ty &)".
1>          with
1>          [
1>              _FwdIt=std::_Vector_iterator<std::_Vector_val<sf::Sprite,std::allocator<sf::Sprite>>>,
1>              _Myvec=std::_Vector_val<sf::Sprite,std::allocator<sf::Sprite>>,
1>              _Ty=std::_Vector_iterator<std::_Vector_val<sf::Sprite,std::allocator<sf::Sprite>>>
1>          ]

回答1:

You're passing a type, SpriteIt, as the last argument to remove instead of the value you want removed from the container.



标签: c++ sfml