我試圖在我的遊戲中爲不同的屏幕創建一個狀態引擎。 id開始屏幕,可能是將來的文件選擇器,overworld地圖,菜單屏幕等。但是當我轉發聲明類時,它說沒有構造函數。類沒有構造函數的前向聲明
GameState.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;
};
GameStates.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);
請提供1)確切的錯誤消息,並附上行號2)出現錯誤的相關代碼段3)決定您希望我們解決的問題:a)編譯錯誤或b)來來回回。 – iksemyonov
用這些類的完整代碼和編譯器錯誤編輯它。 – Vince
你知道編譯器如何從上到下處理你的代碼嗎? – immibis