DirectX 游戏语法错误

DirextX Game Syntax Error

嗯,

我的游戏需要一些帮助。我正在尝试更改介绍中的游戏状态 class 但它给出了很多错误。

Intro.h

#pragma once
#include "SpriteBatch.h"
#include "GameState.h"

class Intro
{
public:
    Intro();
    ~Intro();

    void Update(GameState* gameState);
    void Draw(DirectX::SpriteBatch* spriteBatch);

private:

    float _timer = 0.0f;
};

Intro.cpp

#include "Intro.h"
#include "LoadContent.h"
#include "SimpleMath.h"


Intro::Intro()
{
    LoadContent::InitIntro();
    if (!LoadContent::isLoaded("Block"))
        LoadContent::LoadTexture(L"Images/Block.dds", "Block");
}


Intro::~Intro()
{
}

void Intro::Update(GameState* gameState)
{
    if (_timer < 10)
        _timer += 0.1;
    else
        gameState->SwitchState(GameState::ScreenType::MenuScreen);
}

void Intro::Draw(DirectX::SpriteBatch* spriteBatch)
{
    spriteBatch->Draw(LoadContent::GetTexture("Block"), DirectX::SimpleMath::Vector2(170, 196), DirectX::Colors::White);
}

GameState.h

#pragma once
#include "SpriteBatch.h"
#include "Menu.h"
#include "SwitchScreen.h"
#include "Intro.h"

class GameState
{
public:
    GameState();
    ~GameState();

    void Update();
    void Draw(DirectX::SpriteBatch* spriteBatch, DirectX::SpriteFont* spriteFont);

public:

    enum ScreenType{Direct, MenuScreen, Game};

private:

    ScreenType _screenType;
    Intro* _screen;
    Menu* _menu;
    SwitchScreen* _switch;

public:
    void GameState::SwitchState(ScreenType switchtype);

};

GameState.cpp

#pragma once
#include "GameState.h"


GameState::GameState()
{
    _switch = new SwitchScreen();
    _screenType = ScreenType::Direct;
    SwitchState(_screenType);
}


GameState::~GameState()
{
}

void GameState::Update()
{
    _switch->Update();

    switch (_screenType)
    {
    case GameState::Direct:
        _screen->Update(this);
        break;
    case GameState::MenuScreen:
        _menu->Update();
        break;
    case GameState::Game:
        break;
    default:
        break;
    }
}


void GameState::Draw(DirectX::SpriteBatch* spriteBatch, DirectX::SpriteFont* spriteFont)
{
    switch (_screenType)
    {
    case GameState::Direct:
        _screen->Draw(spriteBatch);
        break;
    case GameState::MenuScreen:
        _menu->Draw(spriteBatch, spriteFont);
        break;
    case GameState::Game:
        break;
    default:
        break;
    }
    _switch->Draw(spriteBatch);
}

void GameState::SwitchState(ScreenType switchtype)
{
    if (!_switch->GetUpdate())
    {
        _switch = new SwitchScreen();
    }
    else {
        if (_switch->GetUpdate())
        {
            _screen = NULL;
            _menu = NULL;

            switch (switchtype)
            {
            case GameState::Direct:
                _screen = new Intro();
                break;
            case GameState::MenuScreen:
                _menu = new Menu();
                break;
            case GameState::Game:
                break;
            default:
                break;
            }
            //_switch->SetSwitch(false);
        }
    }
}

Error 1 error C2061: syntax error : identifier 'GameState' \jelly\intro.h 11

Error 2 error C2061: syntax error : identifier 'GameState' \jelly\intro.h 11

Error 6 error C2061: syntax error : identifier 'GameState' \jelly\intro.h 11

Error 3 error C2143: syntax error : missing ';' before '*' \jelly\gamestate.h 23

Error 7 error C2660: 'Intro::Update' : function does not take 1 arguments \jelly\gamestate.cpp 24

Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int \jelly\gamestate.h 23

Warning 5 warning C4305: '+=' : truncation from 'double' to 'float' \jelly\intro.cpp 21

谢谢

看起来像是一个循环包含问题。

您应该尝试在 Intro.h 头文件中转发声明 GameState

Intro.h

#include "SpriteBatch.h"
// #include "GameState.h" <<< Nope!
class GameState; // Do this instead

而不是在 Intro.h 中包含 GameState.h

相应地在 Intro.cpp 中包含 GameState.h,以便在那里看到完整的 class 声明:

Intro.cpp

#include "Intro.h"
#include "LoadContent.h"
#include "SimpleMath.h"
#include "Gamestate.h" // <<<<

1. Intro.h.

不需要 GameState.h

Intro.h

#pragma once
#include "SpriteBatch.h"
//#include "GameState.h" -> remove this

class GameState; //Add this line - forward declaration is enough to declare a pointer.

2. 添加 GameState.hIntro.cpp.

Intro.cpp

#include "Intro.h"
#include "LoadContent.h"
#include "SimpleMath.h"
#include "GameState.h" //add this

3. GameState.h.

不需要 Intro.h

GameState.h

#pragma once
#include "SpriteBatch.h"
#include "Menu.h"
#include "SwitchScreen.h"
//#include "Intro.h" -> remove this

class Intro; //Add this line - forward declaration is enough to declare a pointer.

4. 添加 Intro.hGameState.cpp.

GameState.cpp

#pragma once
#include "GameState.h"
#include "Intro.h" //add this

5.最大的问题。

GameStateclass中,更改:

public:
  void GameState::SwitchState(ScreenType switchtype);

public:
  void SwitchState(ScreenType switchtype);

这就是您的编译器可能会抱怨的问题。