tags:

views:

83

answers:

3

compiler output:

g++ -Wall -g main.cpp `sdl-config --cflags --libs` -lSDL_mixer
In file included from Game.h:8,
                 from main.cpp:1:
DrawableObject.h:11: error: ISO C++ forbids declaration of ‘Game’ with no type
DrawableObject.h:11: error: expected ‘;’ before ‘*’ token
DrawableObject.h:13: error: expected ‘)’ before ‘*’ token
main.cpp:7: error: expected ‘}’ at end of input
main.cpp:7: error: expected unqualified-id at end of input
make: *** [all] Error 1
brett@brett-laptop:~/Desktop/SDL$ make
g++ -Wall -g main.cpp `sdl-config --cflags --libs` -lSDL_mixer
In file included from Game.h:8,
                 from main.cpp:1:
DrawableObject.h:11: error: ISO C++ forbids declaration of ‘Game’ with no type
DrawableObject.h:11: error: expected ‘;’ before ‘*’ token
DrawableObject.h:13: error: expected ‘)’ before ‘*’ token
main.cpp:7: error: expected ‘}’ at end of input
main.cpp:7: error: expected unqualified-id at end of input
make: *** [all] Error 1

main.cpp:

#include "Game.h"

int main()
{
    Game g;
    return 0;
}

Game.h:

#ifndef GAME_H
#define GAME_H

#include <cmath>
#include "SDL.h"
#include <vector>
#include "DrawableObject.h"

typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;

class Game
{
public:

    SDL_Surface * screen;
    std::vector<DrawableObject*> sprites;

    Game()
    {   
        if (SDL_Init(SDL_INIT_VIDEO) != 0)
            return;

        atexit(SDL_Quit);
        screen = SDL_SetVideoMode(640, 480, 0, SDL_DOUBLEBUF);

        if (screen == NULL)
            return;

        while (true)
        {
            SDL_Event * event;
            while(SDL_PollEvent(event))
            {
                if(event->type == SDL_QUIT)
                    return;
            }

            SDL_LockSurface(screen);

            for (uint32 i=0; i<sprites.size(); ++i)
            {
                sprites[i]->update(event);
            }

            SDL_FreeSurface(screen);
            SDL_Flip(screen);
    }
};

#endif

DrawableObject.h:

#ifndef DRAWABLE_OBJECT_H
#define DRAWABLE_OBJECT_H

#include "SDL.h"
#include "Game.h"

class DrawableObject
{
public:
    Game * game;

    DrawableObject(Game * const game_)
        : game(game_)
        {}

    virtual void update(SDL_Event *event) = 0;

};

#endif
+6  A: 

The problem is that you are missing a closing brace for the while loop in Game.h

Update: As other's have mentioned you also need to resolve you circular include of Game.h in DrawableObject.h. You can simply put a forward declaration of the Game type in the DrawableObject header, see @ybungalobill and @Lou Franco answers for examples.

#ifndef GAME_H 
#define GAME_H 

#include <cmath> 
#include "SDL.h" 
#include <vector> 
#include "DrawableObject.h" 

typedef unsigned char uint8; 
typedef unsigned short uint16; 
typedef unsigned int uint32; 

class Game 
{ 
public: 

    SDL_Surface * screen; 
    std::vector<DrawableObject*> sprites; 

    Game() 
    {    
        if (SDL_Init(SDL_INIT_VIDEO) != 0) 
            return; 

        atexit(SDL_Quit); 
        screen = SDL_SetVideoMode(640, 480, 0, SDL_DOUBLEBUF); 

        if (screen == NULL) 
            return; 

        while (true) 
        { 
            SDL_Event * event; 
            while(SDL_PollEvent(event)) 
            { 
                if(event->type == SDL_QUIT) 
                    return; 
            } 

            SDL_LockSurface(screen); 

            for (uint32 i=0; i<sprites.size(); ++i) 
            { 
                sprites[i]->update(event); 
            } 

            SDL_FreeSurface(screen); 
            SDL_Flip(screen); 
        } // This brace is missing
    } 
}; 

#endif 
Chris Taylor
+1  A: 

You have circular dependency here. When you include Game.h from main.cpp and it includes DrawableObject.h the latter tries to include Game.h but it has no effect since GAME_H is already defined but class Game is not declared yet.

You need to use forward declaration of class Game in DrawableObject.h like this:

// #include "Game.h" <-- remove this since it has no effect
class Game; // <-- forward declaration

class DrawableObject
{
public:
    Game * game;
    // ...
};
ybungalobill
+1  A: 

You probably have a mutual inclusion problem. In DrawableObject.h, change

#include "Game.h"

to

class Game;

A forward declaration is sufficient if you only need pointers and references to a class, which you do.

Lou Franco
And also @Chris Taylor's answer. It think you have both this and the close brace for the while
Lou Franco