c++ - 类没有构造函数前向声明

标签 c++ sdl state

我正在尝试在我的游戏中为不同的屏幕创建一个状态引擎。 id 开始屏幕,可能是 future 的文件选择器、世界地图、菜单屏幕等。但是当我转发声明类时,它说没有构造函数。

游戏状态.h:

#pragma once

class GameState
{
public:
    virtual ~GameState() {}
    virtual void Update() {}
    virtual void HandleEvents() {}
    virtual void Draw(Graphics& gfx) {}

    GameState* getCurrentState()
    {
        return currentState;
    }
    void ChangeState(GameState* state)
    {
        currentState = state;
    }

protected:
    SDL_Renderer* renderer;
    GameState* currentState;
};

游戏状态.h

#pragma once
#include "GameState.h"
#include "Texture.h"
#include "Keyboard.h"

class TitleGameState;
class IntroGameState : public GameState
{
public:
    IntroGameState(SDL_Renderer* renderer, KeyboardClient& kbd)
        :
        kbd(kbd)
    {
        background = new Texture("red.png", renderer);
        this->renderer = renderer;
    }
    ~IntroGameState() {}
    void HandleEvents()
    {
        while (!kbd.KeyEmpty())
        {
            KeyEvent e = kbd.ReadKey();
            switch (e.GetCode())
            {
            case SDLK_RETURN:
                if (e.IsPress())
                {
                    currentState = new TitleGameState(renderer, kbd);
                }
                break;
            }
        }
    }
    void Logic() {}
    void Draw(Graphics& gfx)
    {
        background->Draw(0, 0, gfx);
    }

private:
    Texture* background;
    KeyboardClient& kbd;
};

class TitleGameState : public GameState
{
public:
    TitleGameState(SDL_Renderer* renderer, KeyboardClient& kbd)
        :
        kbd(kbd)
    {
        background = new Texture("blue.png", renderer);
    }
    ~TitleGameState() {}
    void HandleEvents()
    {
        while (!kbd.KeyEmpty())
        {
            KeyEvent e = kbd.ReadKey();
            switch (e.GetCode())
            {
            case SDLK_RETURN:
                if (e.IsPress())
                {
                    printf("OK");
                }
                break;
            }
        }
    }
    void Logic() {}
    void Draw(Graphics&gfx)
    {
        background->Draw(0, 0, gfx);
    }

private:
    Texture* background;
    KeyboardClient& kbd;
};

我随后立即定义类,我知道我可以将它移到 IntroGameState 之上,但是当我实现菜单游戏状态时,它会在菜单状态和主世界状态之间来回切换。我该如何解决这个问题?

编译器错误:

error C2514: 'TitleGameState' : class has no constructors
File: gamestates.h
Line: 28

第28行是这行代码:

currentState = new TitleGameState(renderer, kbd);

最佳答案

currentState = new TitleGameState(renderer, kbd); 行的错误是因为编译器还没有看到该类的构造函数。它不知道如何编译这段代码。

如果您转发声明类class TitleGameState;,您几乎可以声明一个指针TitleGameState*

要编译您拥有的代码,您需要在使用前定义类。注意:定义类并不意味着也定义所有方法。类定义由方法和成员声明组成。

class TitleGameState : public GameState
{
public:
  TitleGameState(SDL_Renderer*, KeyboardClient&);
  ~TitleGameState();
  void HandleEvents();
  // ...
};

class IntroGameState : public GameState
{
  // ...
}

定义完类之后,就可以定义成员函数了;

/*inline*/ void TitleGameState::HandleEvents()
// inline is needed if the method is defined in a header file
// to help avoid linker errors
{
  // ...
}

关于c++ - 类没有构造函数前向声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35263459/

相关文章:

c++ - const T* const 指向特征矩阵

c++ - OpenGL 覆盖 Windows 的按钮

c - 使用 SDL2 的纹理逐像素渲染缓慢

event-handling - 带有多个窗口的 SDL 2.0 退出事件

javascript - 使用状态机使我的 UI 简单灵活?好主意?我如何在 Knockout JS 中做到这一点?

c++ - 获取指向结构的第一个元素的指针

c++ - 通过组合向类添加锁

c++ - 为什么 SDL_Flip 不限制 FPS 以匹配屏幕频率?

html - 删除 HTML 按钮/提交的完整样式

asp.net - 删除列后,Gridview 丢失 ItemTemplate