【问题标题】:Xcode C++ error with game states带有游戏状态的 Xcode C++ 错误
【发布时间】:2012-07-01 21:06:16
【问题描述】:

所以我想制作一款代码非常简洁且组织良好的游戏。调查游戏状态我发现了这个网站:http://gamedevgeek.com/tutorials/managing-game-states-in-c/

使用他的模板,到目前为止我有这个:

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

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

然后每次我尝试实现这些功能时都会出现错误“使用未声明的标识符'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);

}

【问题讨论】:

  • 应该是#define Rect_Game_TitleScreenState_hpp 而不是#def Rect_Game_TitleScreenState_hpp。不过那里可能还有更多问题
  • 让我永远看到了不同之处,但很好。我一定是累了。错误仍然存​​在。

标签: c++ xcode state sfml


【解决方案1】:

这里的问题是TitleScreenState.hpp:

#ifdef Rect_Game_TitleScreenState_hpp
#def Rect_Game_TitleScreenState_hpp

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

【讨论】:

  • 啊哈。我修正了定义,但我没有抓住'n'。这解决了我的主要问题,现在纠正其他错误。
  • @Jazzertron,还有哪些错误?我没有尝试编译你的代码。
  • 与此无关。既然这是正确的,只需修复其他代码。
【解决方案2】:

我有一段时间没有接触过 c++,但您似乎已声明默认构造函数是私有的:

private:
   TitleScreenState() {}

如果我没记错的话,这意味着你不能跑步:

void TitleScreenState::Init()

因为它是一个非静态函数,需要实例化TitleScreenState,并且默认控制器是私有的:S,所以在您拥有new TitleScreenState() 的任何地方都会出现该错误,因为实际上没有可用的公共TitleScreenState给出错误Use of undeclared identifier 'TitleScreenState'

您是否尝试过将其公开?

public:
   TitleScreenState() {}

【讨论】:

  • 我这样做是因为我认为这就是 Singleton 模式的发展方式。也就是说,你只会实例化一次,所以构造函数应该被隐藏,取而代之的是一个返回静态实例的方法 (TitleScreen::Instance())
  • 是的,但是如果您希望执行“new BlaBlaBla()”,则需要有一个公共构造函数。你可能想要一个运行私有构造函数的静态函数
猜你喜欢
  • 2011-03-15
  • 1970-01-01
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
相关资源
最近更新 更多