在sfml 2.1中从vector中删除sprite

时间:2014-07-26 14:03:19

标签: c++ sfml

我正在尝试从矢量中删除精灵,但无济于事,我不知道什么是错的(使用SFML 2.1的VS 2010)。它根本不编译。任何提示赞赏。

这一行似乎有问题:

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

完整代码:

#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;
}

错误日志:

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 个答案:

答案 0 :(得分:1)

您将类型SpriteIt作为remove的最后一个参数传递,而不是要从容器中删除的值。