繁体   English   中英

SDL / C ++包含问题

[英]SDL/C++ include problems

我开始使用SDL库为我的学校项目使用C ++做游戏。 但是我无法在下面的代码中找到我在做什么。

我正在使用VC ++ 2008,

这是编译器的输出:

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;
}

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

在此先感谢您有一个美好的一天:)

#include <SDL.h>移至dec.h。 在编译initGame.cpp时,您从未告诉编译器查看SDL.h,因此它无法弄清楚这些SDL_什么,并且引起了极大的困惑。

另外,请勿#include一个* .cpp文件。 从main.cpp中删除#include "initGame.cpp"

  1. 在init.h中,包括SDL标头和cstring(代表NULL)
  2. 在init.cpp中包含iostream

您缺少包含。

每个cpp文件(称为编译单元)都是独立的,因此您需要为每个cpp文件提供所有定义以及所需的定义。 即,initGame.cpp的dec.h包含项,但sdl.h的没有包含项。

因此:将'include添加到initgame.cpp或dec.h中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM