SDL/C++ include problems
I started doing game for my school project in C++ with SDL library. But I cannot find what am I doing wrong in code below.
I am using VC++ 2008,
here is the compilator output:
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
And here is the source
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;
}
I would be very grateful if somebody could help me.
Thanks in advance a have a nice day :)
Move #include <SDL.h>
to dec.h. When compiling initGame.cpp, you never told the compiler to look at SDL.h, so it couldn't figure out what any of those SDL_
things were and got very confused.
Also, don't #include
one *.cpp file from another. Take the #include "initGame.cpp"
out of main.cpp.
- In init.h include the SDL headers, and cstring for NULL
- in init.cpp include iostream
You're missing the include.
Each cpp file (known as a compilation unit) is self-contained, so you need to give each one all the definitions and suchlike that it needs. ie, initGame.cpp has an include for dec.h, but that doesn't have one for sdl.h.
So: either add the 'include to initgame.cpp or to dec.h.
精彩评论