即使没有以前的声明,也会出现“未命名类型”和“未在此范围内声明”错误

时间:2019-02-15 13:28:06

标签: c++ codeblocks sfml

我完成课堂学习后,其他文件中开始出现错误。我在Google的此处,各处搜索了答案,但是仍然找不到答案。没有一种解决方案有效,我认为这是编译器的错,因为所有声明和包含都在那。有人可以验证一下并告诉我出什么问题了吗?

Statemachine.hpp:

#pragma once
#include <memory>
#include "Data.hpp"

namespace Engine
{
    class Statemachine
    {
        statePtr tempState;
        std::vector<statePtr> runningStates;
        bool isReplacing;

    public:
        void addState(statePtr state,bool isreplacing);
        void removeState();

        statePtr& currentState();
    };
}

Data.hpp:

#pragma once
#include "State.hpp"
#include "Statemachine.hpp"
#include "Assetmanager.hpp"
#include "Input.hpp"
#include "Basicmath.hpp"
#include "Collision.hpp"

#define ROBOTO_FONT_PATH "resources/fonts/Roboto-Regular.ttf"
#define CONFIG_PATH "resources/config.txt"

namespace Engine
{
    typedef std::unique_ptr<State> statePtr;

    struct gameData
    {
        Assetmanager assets;
        Statemachine states;
        Input input;
        Math math;
        CollisionDetection collision;
    };
}

Statemachine.hpp中的错误: 'statePtr'没有命名类型 尚未声明“ statePtr” 以及其他由这些引起的。

Data.hpp中的错误: “状态机”未命名类型

在我完成FPSCounter类之后,这些错误开始发生。后来我尝试从项目中删除这些文件,并且一切正常,执行“状态机”未声明为错误。

FPSCounter.hpp:

#pragma once
#include "SFML/Graphics.hpp"
#include "Data.hpp"

namespace Engine
{
    class FPScounter
    {
        gameData prData;
        float fps;
        int intFps;
        int frames;
        sf::Text text;
        sf::Font font;
        sf::Clock clock;
    public:
        FPScounter(int textSize);
        float calculate();
        void draw(sf::RenderWindow &window);
    };
}

FPSCounter.cpp:

#include "FPScounter.hpp"
#include <math.h>
#include <string>

namespace Engine
{
    FPScounter::FPScounter(int textSize)
    {
        this->prData.assets.loadFont("Roboto",ROBOTO_FONT_PATH);
        this->text.setFont(this->prData.assets.getFont("Roboto"));
        this->text.setPosition(sf::Vector2f(0,0));
        this->text.setCharacterSize(textSize);
    }
    float FPScounter::calculate()
    {
        this->frames++;
        this->fps=this->frames/this->clock.restart().asSeconds();
        if(this->fps-floor(this->fps)>0.5)
        {
            this->intFps=(int)ceil(this->fps);
            return this->intFps;
        }
        else
        {
            this->intFps=(int)floor(this->fps);
            return this->intFps;
        }
    }
    void FPScounter::draw(sf::RenderWindow &window)
    {
        if(this->prData.assets.readFromFile(CONFIG_PATH,"FPSCounterOn")=="true")
        {
            this->text.setString("FPS: " + std::to_string(this->intFps));
            window.draw(text);
        }
    }
}

0 个答案:

没有答案