ISO C ++禁止声明'游戏'没有类型 - 可能包括问题?

时间:2010-10-03 16:26:00

标签: c++ sdl

编译器输出:

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

3 个答案:

答案 0 :(得分:6)

问题是你缺少Game.h中while循环的右括号

更新:正如其他人提到的那样,您还需要在Game.h中解决DrawableObject.h的循环播放问题。您只需在DrawableObject标头中放置Game类型的前向声明,请参阅@ybungalobill@Lou Franco答案。

#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 

答案 1 :(得分:1)

这里有循环依赖。当您从main.cpp中包含Game.h并且它包含DrawableObject.h时,后者会尝试包含Game.h但它没有任何效果,因为GAME_H已经定义但是Game类尚未声明。

你需要在DrawableObject.h中使用类Game的前向声明,如下所示:

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

class DrawableObject
{
public:
    Game * game;
    // ...
};

答案 2 :(得分:1)

您可能存在相互包含问题。在DrawableObject.h中,更改

#include "Game.h"

class Game;

如果您只需要指针和类的引用,那么前向声明就足够了。