Search code examples
c++oopsdl

Syntax error in c++ default-int


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?


Solution

  • 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