簡體   English   中英

DirextX游戲語法錯誤

[英]DirextX Game Syntax Error

y

我的游戲需要一些幫助。 我正在嘗試在介紹性類中更改gamestate,但它給出了很多錯誤。

簡介

#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;
};

簡介

#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);
        }
    }
}

錯誤1錯誤C2061:語法錯誤:標識符'GameState'\\ jelly \\ intro.h 11

錯誤2錯誤C2061:語法錯誤:標識符'GameState'\\ jelly \\ intro.h 11

錯誤6錯誤C2061:語法錯誤:標識符'GameState'\\ jelly \\ intro.h 11

錯誤3錯誤C2143:語法錯誤:缺少';' 在'*'\\ jelly \\ gamestate.h 23之前

錯誤7錯誤C2660:'Intro :: Update':函數未使用1個參數\\ jelly \\ gamestate.cpp 24

錯誤4錯誤C4430:缺少類型說明符-假定為int。 注意:C ++不支持default-int \\ jelly \\ gamestate.h 23

警告5警告C4305:'+ =':從'double'到'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 ,以便在那里看到完整的類聲明:

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

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.h include添加到GameState.cpp

GameState.cpp

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

5.最大的問題。

GameState類中,更改:

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

public:
  void SwitchState(ScreenType switchtype);

這就是您的編譯器可能抱怨的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM