c – 构建SFML蓝图小行星游戏时调试断言失败?

c – 构建SFML蓝图小行星游戏时调试断言失败?,第1张

概述我试图遵循SFML蓝图书.我设法删除所有错误,但代码中仍然存在一个错误. 我试图找到debug-assertion-failed. 我认为这个问题是由于其中一个清单被清空而引起的. 我的world.cpp代码 #include "World.h"#include "Entity.h"World::World(int x, int y) : _x(x), _y(y) {}World::~Wo 我试图遵循SFML蓝图书.我设法删除所有错误,但代码中仍然存在一个错误.
我试图找到deBUG-assertion-Failed.
我认为这个问题是由于其中一个清单被清空而引起的.

我的world.cpp代码

#include "World.h"#include "Entity.h"World::World(int x,int y) : _x(x),_y(y) {}World::~World() { clear(); }voID World::add(Entity* entity){_entitIEs_tmp.push_back(entity);}voID World::clear(){for (Entity* entity : _entitIEs)    delete entity;_entitIEs.clear();for (Entity* entity : _entitIEs_tmp)    delete entity;_entitIEs_tmp.clear();_sounds.clear();}voID World::add(Configuration::Sounds sound_ID){std::unique_ptr<sf::Sound> sound(new    sf::Sound(Configuration::sounds.get(sound_ID)));sound->setAttenuation(0);sound->play();_sounds.emplace_back(std::move(sound));}bool World::isCollIDe(const Entity& other){for (Entity* entity_ptr : _entitIEs)    if (other.isCollIDe(*entity_ptr))        return true;    return false;}int World::size() {return _entitIEs.size() + _entitIEs_tmp.size();}int World::getX() const{return _x;}int World::getY() const{return _y;}const std::List<Entity*> World::getEntitIEs() const {return _entitIEs;}voID World::update(sf::Time deltaTime){if (_entitIEs_tmp.size() > 0)    _entitIEs.merge(_entitIEs_tmp);for (Entity* entity_ptr : _entitIEs){    Entity& entity = *entity_ptr;    entity.update(deltaTime);    sf::Vector2f pos = entity.getposition();    if (pos.x < 0)    {        pos.x = _x;        pos.y = _y - pos.y;    }    else if (pos.x > _x)    {        pos.x = 0;        pos.y = _y - pos.y;    }    if (pos.y < 0)        pos.y = _y;    else if (pos.y > _y)        pos.y = 0;    entity.setposition(pos);}const auto end = _entitIEs.end();for (auto it_i = _entitIEs.begin(); it_i != end; ++it_i){    Entity& entity_i = **it_i;    auto it_j = it_i;    it_j++;    for (; it_j != end; ++it_j)    {        Entity& entity_j = **it_j;        if (entity_i.isAlive() && entity_i.isCollIDe(entity_j))            entity_i.onDestroy();        if (entity_j.isAlive() && entity_j.isCollIDe(entity_i))            entity_j.onDestroy();    }}for (auto it = _entitIEs.begin(); it != _entitIEs.end();){    if (!(*it)->isAlive()){        delete *it;        it = _entitIEs.erase(it);    }    else ++it;}_sounds.remove_if([](const std::unique_ptr<sf::Sound>& sound)->    bool {    return sound->getStatus() != sf::SoundSource::Status::Playing;});}voID World::draw(sf::rendertarget& target,sf::RenderStates states) const{for (Entity* entity : _entitIEs)    target.draw(*entity,states);}

world.hpp的代码

#include <SFML/Graphics.hpp>#include <SFML/Audio.hpp>#include <List>#include <memory>#include "Configuration.h"class Entity;class World : public sf :: Drawable{public:World(const World&) = delete;World& operator=(const World&) = delete;World(int x,int y);~World();voID add(Entity* entity);voID clear();bool isCollIDe(const Entity& other);int size();voID add(Configuration::Sounds sound_ID);const std::List<Entity*> getEntitIEs() const;int getX() const;int getY() const;voID update(sf::Time deltaTime);private:std::List<Entity*> _entitIEs;std::List<Entity*> _entitIEs_tmp;std::List<std::unique_ptr<sf::Sound>> _sounds;virtual voID draw(sf::rendertarget& target,sf::RenderStates states) const overrIDe;const int _x;const int _y;};

Entity.h的代码

class World;class Entity : public sf::Drawable{public://ConstructorsEntity(const Entity&) = delete;Entity& operator= (const Entity&) = delete;Entity(Configuration::Textures tex_ID,World& world);virtual ~Entity();//Helpersvirtual bool isAlive() const;const sf::Vector2f& getposition() const;template<typename ... Args>voID setposition(Args&& ... args);virtual bool isCollIDe(const Entity& other) const = 0;//Updatesvirtual voID update(sf::Time deltaTime) = 0;virtual voID onDestroy();protected:frIEnd class Meteor;frIEnd class Player;frIEnd class Saucer;frIEnd class ShootPlayer;frIEnd class ShootSaucer;sf::Sprite _sprite;sf::Vector2f _impulse;World& _world;bool _alive;private:virtual voID draw(sf::rendertarget& target,sf::RenderStates states) const overrIDe;};
解决方法
if (_entitIEs_tmp.size() > 0)    _entitIEs.merge(_entitIEs_tmp);

这不起作用. merge适用于两个已排序的范围.而且你的矢量没有排序.这就是你得到断言的原因.

如果没有排序它们是故意的,你只想连接两个向量,你可以这样做:

_entitIEs.reserve( _entitIEs.size() + _entitIEs_tmp.size() );_entitIEs.insert( _entitIEs.end(),_entitIEs_tmp.begin(),_entitIEs_tmp.end() );
总结

以上是内存溢出为你收集整理的c – 构建SFML蓝图小行星游戏时调试断言失败?全部内容,希望文章能够帮你解决c – 构建SFML蓝图小行星游戏时调试断言失败?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1224844.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存