ISO C++禁止声明“游戏”,没有类型-可能包含问题?
编译器输出:ISO C++禁止声明“游戏”,没有类型-可能包含问题?,c++,sdl,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: expecte
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;
}
游戏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
问题是你在游戏中缺少了while循环的结束括号 更新:正如其他人提到的,您还需要解决DrawableObject.h中Game.h的循环包含问题。您只需在DrawableObject标题中声明游戏类型,请参见@ybungalobill和@Lou Franco answers以获取示例
#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
这里有循环依赖关系。当您从main.cpp中包含Game.h并且它包含DrawableObject.h时,后者尝试包含Game.h,但它没有效果,因为Game_h已经定义,但是类Game尚未声明 您需要在DrawableObject.h中使用类游戏的正向声明,如下所示:
// #include "Game.h" <-- remove this since it has no effect
class Game; // <-- forward declaration
class DrawableObject
{
public:
Game * game;
// ...
};
你可能有一个相互包容的问题。在DrawableObject.h中,更改
#include "Game.h"
到
如果只需要指向类的指针和引用,那么向前声明就足够了。以及@Chris Taylor的答案。它认为你既有这个,也有一个紧闭的大括号,顺便说一句,这是一个错误:SDL_事件*事件;而SDL_PollEventevent。。。没有为事件结构分配内存。你想要SDL_事件;当SDL_轮询事件和事件时。。。
#include "Game.h"
class Game;