编译输出:
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
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include "Game.h"
int main()
{
Game g;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
问题是你缺少whileGame.h中循环的右括号
更新:由于对方所提到的,你还需要解决您的圆形包括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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3118 次 |
| 最近记录: |