2010-11-01 48 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