0

So I am making a game trying to use OOP but visual studio isn't happy I am using VC++ 2010 express as my IDE, I decided to "Objectify" SDL into reusable components but now I went from 0 errors to 122 and some of them really aren't errors like when it says window is not defined and i click to go to the error the error disappears... so I am having a bit of trouble

#ifndef GUARD_WINDOWSTATE_H
#define GUARD_WINDOWSTATE_H

#include<SDL.h>
#include"Window.h"
#include"Graphics.h"

/* 
WindowSate is a component to a window class, a windowstate is a seperate
frame of a window, it could be a menu or a level or something
a windowstate handles its events through a window and draws via graphics buffer to
the window's surface it will hold the components of the window(graphics, GUI     components, etc.)
and the window will hold it
*/

class WindowState {
public:

WindowState(Window* window) 
    :m_window(window)
{
    m_window->add_state(this);
}

virtual void load() = 0;

virtual void update( SDL_Event* e) = 0;
virtual void logic() = 0;
virtual void render(Graphics g) = 0;

virtual void unload() = 0;

protected:

Window* m_window;

};

#endif

it tells me that window is undefined when it is defined in window.h which is included here is window.h:

#ifndef GUARD_WINDOW_H
#define GUARD_WINDOW_H

#include"SDL.h"
#include<vector>
#include<string>
#include"WindowState.h"
#include"Graphics.h"
/*The window class is the interface to the user,
it displays everything and is the top layer of the interactive application
it recieves events and passes them down the chain to states and components
it holds states and enumerates through them
*/

class Window {
private:

//width and height of the window
int m_width, m_height;

//graphics handler of the window
Graphics m_g;

//window surface
SDL_Surface* m_surface;

//event to hold all events
SDL_Event m_events;

//current and qeued states
int m_current_state;
int m_queued_state;

//Current WindowState
WindowState* m_window_state;

//is the window open?
bool m_running;

//title of the window
std::string m_title;

//vector to hold all the window states
std::vector<WindowState*> m_states;

public:
//Basic constructor
Window(int width, int height);
//constructor with title
Window(int width, int height, std::string title);

//returns the windows events
virtual SDL_Event& get_events() { return m_events; }

SDL_Surface* surface() { return m_surface; }

bool is_running() { return m_running; }

virtual void run(); //starts the window

virtual void update();    //updates the events 

virtual void update_state();    //checks for a change in state

virtual void draw();    //draws current state

virtual void close(); // closes the window

void queue_window_state(int state);    //sets up a window state to be activated

void add_state(WindowState* state);    //adds a state to the window

void set_caption(std::string caption);
//destructor
~Window();
};

#endif
Charles
  • 50,943
  • 13
  • 104
  • 142
Sbot
  • 37
  • 4

2 Answers2

2

you have a circular include. Notice how WindowState.h includes Window.h and Window.h includes WindowState.h? It's a never ending dependency cycle that the compiler doesn't like.

If it were my money, I'd remove the #include <Window.h form WindowState.h and before the class somewhere do what is called a forward declaration: class Window;.

Keep in mind, you can' t use this class in this file. You can, however, include it and use it in the corresponding WindowState.cpp file.

Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
0

In WindowsState.h both Window and Graphics do not need a complete definition, just a forward declaration, so change:

#include"Window.h"
#include"Graphics.h"

to:

class Window;
class Graphics;

In Window.h, WindowState only needs a forward declaration, so change:

#include "WindowsState.h"

to:

class WindowState;

Graphics cannot be forward-declared in Window.h because the full size of the type must be computed to compile Window successfully.

Using forward declarations minimizes compilation dependencies between files and makes your code compile faster. Changing the Graphics.h and Window.h files, for example, will not require recompiling all files that include only WindowState.h, for example.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Can you explain why Graphics.h cannot be forward-declared? thank you btw – Sbot Jun 15 '13 at 02:59
  • It's not a pointer, which has a known size, and it is part of a class definition, so the compiler needs to know the size to compute the offsets of the class's data members. – Mark Tolonen Jun 15 '13 at 08:04
  • Clarification: `Graphics m_g` is part of `class Window` and is not a pointer.... – Mark Tolonen Jun 15 '13 at 08:12