0
votes

Ey,

I need some little help with my game. I'm trying to change the gamestate in the intro class but it gives alot of errors.

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

Thank you

2
Looks like a circular include issue.πάντα ῥεῖ

2 Answers

0
votes

Looks like a circular include issue.

You should try just forward declaring GameState in your Intro.h header file

Intro.h

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

instead of including GameState.h in Intro.h.

Include GameState.h in Intro.cpp accordingly, to have the full fledged class declaration seen there:

Intro.cpp

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

1. You don't need GameState.h in Intro.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. Add include of GameState.h to Intro.cpp.

Intro.cpp

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

3. You don't need Intro.h in GameState.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. Add include of Intro.h to GameState.cpp.

GameState.cpp

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

5. The biggest issue.

In GameState class, change:

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

to

public:
  void SwitchState(ScreenType switchtype);

This is the thing, that your compiler is probably complaining about.