如何修复调试断言失败?

时间:2019-04-26 23:57:00

标签: c++ visual-c++ stack sfml deque

我正在使用Tic-Tac-Toe SFML C ++ [STATE CREATION]教程中Sonar Systems提供的相同代码制作Tic-Tac-Toe游戏。 但是游戏无法运行,相反,我收到一个消息框,指出调试断言失败,并且在输出窗口中引发了异常:无法超出范围的deque迭代器,并且(有时并非总是)当我尝试再次运行它时输出窗口中会显示以下附加消息:“无效参数已传递给认为无效参数致命的函数”

我安装了sfml并将其成功链接到我的VS2017项目,项目文件中的主体包含了所有必需的资源,并且我确保所有目录路径都正确。我修复了最初的86个错误(关于其他头文件),并且此刻没有红色下划线显示任何代码,只是出现了此消息框。

这是导致相同问题的最少代码:

main.cpp

#include "pch.h"
#include <iostream>
#include "Game.h"

int main()
{
    TicTacToe::Game(800, 600, "Tic-Tac-Toe Game");
    return EXIT_SUCCESS;
}

Game.cpp

#include "pch.h"
#include "Game.h"

namespace TicTacToe
{
    Game::Game(int width, int height, std::string title)
    {
        _data->window.create(sf::VideoMode(width, height), title, sf::Style::Close | sf::Style::Titlebar);

        this->Run();
    }

    void Game::Run()
    {
        float newTime, frameTime, interpolation;
        float currentTime = this->_clock.getElapsedTime().asSeconds();
        float accumulator = 0.0f;
        while (this->_data->window.isOpen())
        {
            this->_data->machine.ProcessStateChanges();
            newTime = this->_clock.getElapsedTime().asSeconds();
            frameTime = newTime - currentTime;

            if (frameTime > 0.25f)
            {
                frameTime = 0.25f;
            }

            currentTime = newTime;
            accumulator += frameTime;

            while (accumulator >= dt)
            {
                this->_data->machine.GetActiveState()->HandleInput();
                this->_data->machine.GetActiveState()->Update(dt);

                accumulator -= dt;
            }

            interpolation = accumulator / dt;
            this->_data->machine.GetActiveState()->Draw(interpolation);
        }
    }
}

Game.h

#pragma once
#include <memory>
#include <string>
#include <SFML\Graphics.hpp>
#include "StateMachine.h"

namespace TicTacToe
{
    struct GameData
    {
        StateMachine machine;
        sf::RenderWindow window;
    };

    typedef std::shared_ptr<GameData> GameDataRef;

    class Game
    {
    public:
        Game(int width, int height, std::string title);
    private:
        const float dt = 1.0f / 60.0f;
        sf::Clock _clock;

        GameDataRef _data = std::make_shared<GameData>();

        void Run();
    };
}

StateMachine.cpp

#include "pch.h"
#include "StateMachine.h"

namespace TicTacToe
{
    void StateMachine::AddState(StateRef newState, bool isReplacing)
    {
        this->_isAdding = true;
        this->_isReplacing = isReplacing;

        this->_newState = std::move(newState);
    }

    void StateMachine::RemoveState()
    {
        this->_isRemoving = true;
    }

    void StateMachine::ProcessStateChanges()
    {
        if (this->_isRemoving && !this->_states.empty())
        {
            this->_states.pop();

            if (!this->_states.empty())
            {
                this->_states.top()->Resume();
            }

            this->_isRemoving = false;
        }

        if (this->_isAdding)
        {
            if (!this->_states.empty())
            {
                if (this->_isReplacing)
                {
                    this->_states.pop();
                }
                else
                {
                    this->_states.top()->Pause();
                }
            }

            this->_states.push(std::move(this->_newState));
            this->_states.top()->Init();
            this->_isAdding = false;
        }
    }

    StateRef &StateMachine::GetActiveState()
    {
        return this->_states.top();
    }
}

StateMachine.h

#include "pch.h"
#include "StateMachine.h"

namespace TicTacToe
{
    void StateMachine::AddState(StateRef newState, bool isReplacing)
    {
        this->_isAdding = true;
        this->_isReplacing = isReplacing;

        this->_newState = std::move(newState);
    }

    void StateMachine::RemoveState()
    {
        this->_isRemoving = true;
    }

    void StateMachine::ProcessStateChanges()
    {
        if (this->_isRemoving && !this->_states.empty())
        {
            this->_states.pop();

            if (!this->_states.empty())
            {
                this->_states.top()->Resume();
            }

            this->_isRemoving = false;
        }

        if (this->_isAdding)
        {
            if (!this->_states.empty())
            {
                if (this->_isReplacing)
                {
                    this->_states.pop();
                }
                else
                {
                    this->_states.top()->Pause();
                }
            }

            this->_states.push(std::move(this->_newState));
            this->_states.top()->Init();
            this->_isAdding = false;
        }
    }

    StateRef &StateMachine::GetActiveState()
    {
        return this->_states.top();
    }
}

状态.h

#pragma once
namespace TicTacToe
{
    class State
    {
    public:
        virtual void Init() = 0;
        virtual void HandleInput() = 0;
        virtual void Update(float dt) = 0;
        virtual void Draw(float dt) = 0;
        virtual void Pause() { }
        virtual void Resume() { }


    };
}

在调用堆栈窗口中,这些是崩溃的行:

StateMachine.cpp(第53-56行)

StateRef &StateMachine::GetActiveState()
    {
        return this->_states.top();
    }

Game.cpp(第41行)

this->_data->machine.GetActiveState()->Draw(interpolation);

Game.cpp(第6-11行)

Game::Game(int width, int height, std::string title)
    {
        _data->window.create(sf::VideoMode(width, height), title, sf::Style::Close | sf::Style::Titlebar);

            this->Run();
    }

main.cpp(第7行)

TicTacToe::Game(800, 600, "Tic-Tac-Toe Game");

我不知道这可能有什么用,但以防万一,我还进入输出窗口,无法找到或打开一些PDB文件,所以我尝试修复它并加载了其中的一些文件,但是仍然有些无法加载。

P.S。我是编程新手

0 个答案:

没有答案