C++ Xcode C++;游戏状态错误

C++ Xcode C++;游戏状态错误,c++,xcode,state,sfml,C++,Xcode,State,Sfml,所以我想制作一个代码非常干净、组织良好的游戏。通过查看游戏状态,我发现了以下网站: 使用他的模板,到目前为止,我有以下几点: GameState.hpp #ifndef Rect_Game_GameState_hpp #define Rect_Game_GameState_hpp #include <SFML/Graphics.hpp> #include "GameEngine.hpp" class GameEngine; class GameState { public:

所以我想制作一个代码非常干净、组织良好的游戏。通过查看游戏状态,我发现了以下网站:

使用他的模板,到目前为止,我有以下几点:

GameState.hpp

#ifndef Rect_Game_GameState_hpp
#define Rect_Game_GameState_hpp

#include <SFML/Graphics.hpp>
#include "GameEngine.hpp"

class GameEngine;

class GameState {

public:
    virtual void Init() = 0;
    virtual void Cleanup() = 0;

    virtual void Pause() = 0;
    virtual void Resume() = 0;

    virtual void HandleEvents(GameEngine* game) = 0;
    virtual void Update(GameEngine* game) = 0;
    virtual void Draw(GameEngine* game) = 0;

    virtual void ChangeState(GameEngine* game, GameState* state);

private: 
    GameState() { }
};

#endif
\ifndef Rect\u Game\u Game state\u hpp
#定义Rect_Game_GameState_hpp
#包括
#包括“GameEngine.hpp”
类游戏引擎;
类配子态{
公众:
虚拟void Init()=0;
虚拟空洞清理()=0;
虚空暂停()=0;
虚拟void Resume()=0;
虚拟虚空句柄事件(GameEngine*game)=0;
虚拟空间更新(游戏引擎*游戏)=0;
虚拟虚空绘制(游戏引擎*游戏)=0;
虚拟状态(游戏引擎*游戏,游戏状态*状态);
私人:
配子状态(){}
};
#恩迪夫
TitleScreenState.hpp

#ifdef Rect_Game_TitleScreenState_hpp
#def Rect_Game_TitleScreenState_hpp

#include <iostream>
#include <SFML/Graphics.hpp>
#include "GameState.hpp"
#include "GameEngine.hpp"

class TitleScreenState : public GameState {

public:
    void Init();
    void Cleanup();

    void Pause();
    void Resume();

    void HandleEvents(GameEngine* engine);
    void Update(GameEngine* engine);
    void Draw(GameEngine* engine);

    void ChangeState(GameEngine* engine, GameState* state);

    static TitleScreenState* Instance();

private:

    TitleScreenState() {}

    static TitleScreenState* titleScreenInstance;

    sf::RenderWindow* window;

    int mouseX;
    int mouseY;

    Button* playButton;

};

#endif
\ifdef Rect\u Game\u TitleScreenState\u水电站
#定义正确的游戏标题说明水电站
#包括
#包括
#包括“GameState.hpp”
#包括“GameEngine.hpp”
类标题Screenstate:公共游戏状态{
公众:
void Init();
空洞清理();
无效暂停();
作废简历();
无效句柄事件(游戏引擎*引擎);
无效更新(游戏引擎*引擎);
无效绘制(游戏引擎*引擎);
void ChangeState(游戏引擎*引擎,游戏状态*状态);
静态TitleScreenState*实例();
私人:
TitleScreenState(){}
静态标题筛选状态*标题筛选状态;
sf::RenderWindow*窗口;
int-mouseX;
int-mouseY;
按钮*播放按钮;
};
#恩迪夫
然后每次我尝试实现函数时都会出现错误“使用未声明的标识符'TitleScreenState'”。它也不是自动完成“TitleScreenState”。有什么建议吗

TitleScreenState.cpp

#include <iostream>
#include "GameEngine.hpp"
#include "GameState.hpp"
#include "TitleScreenState.hpp"
#include "ResourcePath.hpp"



void TitleScreenState::Init()
{
    // Initialize values
    leftClick = false;
    mouseX = 0;
    mouseY = 0;

    // Load title screen image
    sf::Texture titleImage;
    if (!titleImage.loadFromFile(resourcePath() + "TitleScreen.png"))
        printf("could not load TitleScreen.png");

    sf::Sprite titleScreen;
    titleScreen.setTexture(titleImage);

    playButton = new Button("Play", 350, 220);

}
#包括
#包括“GameEngine.hpp”
#包括“GameState.hpp”
#包括“TitleScreenState.hpp”
#包括“ResourcePath.hpp”
void TitleScreenState::Init()
{
//初始化值
leftClick=false;
mouseX=0;
mouseY=0;
//加载标题屏幕图像
sf::纹理标题图像;
如果(!titleImage.loadFromFile(resourcePath()+“TitleScreen.png”))
printf(“无法加载TitleScreen.png”);
sf::雪碧标题屏幕;
titleScreen.setTexture(标题图像);
playButton=新按钮(“Play”,350,220);
}

> p>我在一段时间内没有接触过C++,但似乎已经声明默认构造函数是私有的:

private:
   TitleScreenState() {}
如果我没弄错,这意味着你不能跑步:

void TitleScreenState::Init()
因为它是一个非静态函数,需要实例化
TitleScreenState
,并且默认控制器是private:S,所以无论您在哪里有
new TitleScreenState()
都会出现该错误,因为实际上没有公共
TitleScreenState
可用,从而导致错误
使用未声明的标识符“TitleScreenState”

你试过把它公之于众吗

public:
   TitleScreenState() {}

这里的问题是TitleScreenState.hpp:

#ifdef Rect_Game_TitleScreenState_hpp
#def Rect_Game_TitleScreenState_hpp

首先,
#ifdef
阻止编译器查看文件的其余部分,因为尚未定义
Rect_Game_TitleScreenState_hpp
。因此,它应该是
#ifndef
。此外,
#def
应该是
#define
。这应该可以解决问题。

应该是“定义正确的游戏标题声明”hpp,而不是“定义正确的游戏标题声明”hpp。可能还有更多的问题,虽然我花了很长时间才看到区别,但这是一个很好的发现。我一定累了。但是错误仍然存在,我这样做是因为我认为这就是单例模式的运作方式。i、 例如,您将只实例化它一次,因此构造函数应该是隐藏的,并且应该是一个返回静态实例(TitleScreen::instance())的方法来代替它。是的,但是如果您希望执行“new BlaBlaBla()”,您需要有一个公共构造函数。您可能需要一个运行私有构造函数oraha的静态函数。我修正了定义,但没有捕捉到“n”。这解决了我的主要问题,现在纠正其他错误。@Jazzertron,其他错误是什么?我还没有试着编译你的代码。与此无关。现在这是正确的,只需修复其他代码。