2010-11-01 91 views
1

我开始使用SDL库在C++中为我的学校项目开发游戏。但我无法找到我在下面的代码中做错了什么。SDL/C++包含问题

我使用VC++ 2008,

这里是compilator输出:

1>Compiling... 
1>initGame.cpp 
1>.\dec.h(4) : error C2065: 'SDL_HWSURFACE' : undeclared identifier 
1>.\dec.h(4) : error C2065: 'SDL_DOUBLEBUF' : undeclared identifier 
1>.\dec.h(6) : error C2143: syntax error : missing ';' before '*' 
1>.\dec.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>.\dec.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>.\dec.h(6) : error C2065: 'NULL' : undeclared identifier 
1>.\initgame.cpp(4) : error C2065: 'SDL_INIT_VIDEO' : undeclared identifier 
1>.\initgame.cpp(4) : error C2065: 'SDL_INIT_AUDIO' : undeclared identifier 
1>.\initgame.cpp(4) : error C2065: 'SDL_INIT_TIMER' : undeclared identifier 
1>.\initgame.cpp(4) : error C3861: 'SDL_Init': identifier not found 
1>.\initgame.cpp(5) : error C2065: 'cerr' : undeclared identifier 
1>.\initgame.cpp(5) : error C2065: 'endl' : undeclared identifier 
1>.\initgame.cpp(9) : error C3861: 'SDL_SetVideoMode': identifier not found 

这里是源

dec.h:

#ifndef DEC_H 
#define DEC_H 

int g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF; 

SDL_Surface *g_screen = NULL; 

#endif 

initGame.cpp :

#include "dec.h" 

bool initGame(){ 
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) == -1){ 
    cerr << "Unable to initialize SDL" << endl; 
    return false; 
} 

g_screen = SDL_SetVideoMode(640, 480, 0, g_win_flags); 

return true; 
} 

main.cpp中:

#include <iostream> 
#include <SDL.h> 

#pragma comment(lib, "SDL.lib") 
#pragma comment(lib, "SDLmain.lib") 

using namespace std; 

#include "initGame.cpp" 

int main(int argc, char *argv[]){ 

initGame(); 
return 0; 
} 

我将不胜感激,如果有人能帮助我。

在此先感谢有一个愉快的一天:)

+0

@Zod - 解开你重新标记。这是一个C++问题。这个问题不是特定于visual C++的。 – 2010-11-01 19:44:25

+0

哦,男人,在这段代码中有这么多小事情向我提出警告。有些人可能会称它为代码风格。 – 2010-11-01 20:37:52

回答

4

#include <SDL.h>移动到dec.h.当编译initGame.cpp时,你从来没有告诉编译器看SDL.h,所以它不知道这些东西是什么,变得非常困惑。

另外,请勿从另一个* .cpp文件中获取#include文件。取出main.cpp中的#include "initGame.cpp"

+0

感谢您的回答。但我有点困惑,为什么我应该从main.cpp中包含#include“initGame.cpp”?那么我将如何访问initGame? – peto1234 2010-11-01 19:43:03

+1

你应该在头文件中放置一个'initGame'的_declaration_(像dec.h或一个新文件initGame.h):'bool initGame();' – aschepler 2010-11-01 19:44:08

1

你错过了包括。

每个cpp文件(称为编译单元)都是自包含的,因此您需要为每个cpp文件提供所需的所有定义和类似内容。即initGame.cpp有一个dec.h的include,但是没有一个用于sdl.h.

所以:要么添加'包括initgame.cpp或dec.h.

2
  1. 在init.h中包括SDL头, 和init.cpp为c_string为NULL
  2. 包括的iostream