-3

So I just began programming in c++ and I am planning to make a small game as my first project (with the SDL library).

So I have this really small piece of code in a header file that gives me an error that I can not solve.

main.h

#include "Screen.h"
#include "MainGame.h"

Screen* screen = nullptr; //main.h line 8

Screen.h

#pragma once

#include "SDL.h"
#include <string>
#include <stdio.h>
#include <iostream>
#include "GameState.h"
#include "Game.h"
using namespace std;

class Screen
{

public:
    Screen(string name, int width, int height, GameState* state);
    ~Screen();
    SDL_Window *window;
    SDL_Surface *screen;
    SDL_Renderer *renderer;
};

MainGame.h

#pragma once
#include "GameState.h"
#include <stdio.h>

class MainGame :
    public GameState
{
public:
    MainGame();
    ~MainGame();
    void start();
    void update();
    void render();
    void stop();
};

Game.h

#pragma once
#include "GameState.h"
#include "Screen.h"
#include "SDL.h"
#include "main.h"

class Game
{
public:
    GameState* activestate;
    Game(GameState state);
    ~Game();
    void changeState(GameState newState);
    bool isRunning;
    void handleEvents();
    void update();
    void render();
    void stop();
};

GameState.h

#pragma once
class GameState
{ 
public:
    GameState();
    ~GameState();
    virtual void start();
    virtual void update();
    virtual void stop();
    virtual void render();
};

And it gives this errors:

Error   C2143   syntax error: missing ';' before '* main.h  8

Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    main.h  8   

What do these errors mean and how do I solve them?

Student
  • 805
  • 1
  • 8
  • 11
Jakibah
  • 53
  • 6
  • 2
    Post the contents of the other files. The error messages are pointing to line 8 of main.h but you posted 6 lines of code – Thomas Sablik Jul 22 '18 at 20:47
  • 2
    Hard to tell without more context. Probably an error from the included headers. – πάντα ῥεῖ Jul 22 '18 at 20:48
  • Edited and added the code. – Jakibah Jul 22 '18 at 20:54
  • Now of course we need to see the other header file contents. –  Jul 22 '18 at 20:56
  • 1
    Does `Game.h` or `GameState.h` include `main.h`? because if so then you are using the name `Screen` before it has been defined. That would explain the error message you see. Try to avoid recursive includes, where A.h includes B.h which includes A.h, not illegal but leads to much confusion. – john Jul 22 '18 at 20:58
  • Yes `Game.h` includes `main.h` (and for no good reason), that's your problem. Only include in a header the minimum needed to get that header to compile. `Game.h` only needs to include `GameState.h` nothing more. – john Jul 22 '18 at 21:02
  • You can try forward declaration of `Screen`. Write `class Screen;` in main.h:7 – Thomas Sablik Jul 22 '18 at 21:03
  • The error comes from your circular dependency. Some file includes Screen.h. Screen.h includes Game.h. Game.h includes main.h. main.h needs Screen.h but because of pragma once it can't include it. So it doesn't know Screen. Either remove circular dependency or use forward declaration. – Thomas Sablik Jul 22 '18 at 21:07

1 Answers1

1

The error comes from your circular dependency. Some file includes Screen.h. Screen.h includes Game.h. Game.h includes main.h. main.h needs Screen.h but because of pragma once it can't include it. So it doesn't know class Screen. Either remove circular dependency (better solution if possible, here it's possible):

Remove #include "main.h" in Game.h

or use forward declaration:

Write class Screen; in main.h:7

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • 2
    Remove the circular dependency would be my preference. Especially as it's not actually needed. There's no reason for Game.h to include main.h. – john Jul 22 '18 at 21:09