我正在尝试使用SDL2.0
和visual studio 2010 express edition
在c ++中制作游戏。我想创建一个类来保存关于某个级别的所有信息,例如地图结构,起始位置,完成位置,加电位置。这个类用在多个源文件中,所以我创建了一个新的.cpp文件,我将在其中存储该类和一个声明该类的.h文件。问题是它不能编译。我收到以下错误:
1>------ Build started: Project: Game, Configuration: Debug Win32 ------
1> gameMain.cpp
1>c:\users\dennis\documents\visual studio 2010\projects\therealmofinvisiblemazes_game\game\gamemain.cpp(33): error C2079: 'GameMap' uses undefined class 'level'
1>c:\users\dennis\documents\visual studio 2010\projects\therealmofinvisiblemazes_game\game\gamemain.cpp(36): error C2664: 'gameInit' : cannot convert parameter 3 from 'int *' to 'level *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> gameLevelObject.cpp
1> gameInit.cpp
1>c:\users\dennis\documents\visual studio 2010\projects\therealmofinvisiblemazes_game\game\gameinit.cpp(27): error C2027: use of undefined type 'level'
1> c:\users\dennis\documents\visual studio 2010\projects\therealmofinvisiblemazes_game\game\gamelevelobject.h(3) : see declaration of 'level'
1>c:\users\dennis\documents\visual studio 2010\projects\therealmofinvisiblemazes_game\game\gameinit.cpp(27): error C2440: '<function-style-cast>' : cannot convert from 'short' to 'level'
1> Source or target has incomplete type
1> Generating Code...
实际游戏是作为DLL创建的,因此我可以向主体添加菜单或视频等内容。 这些是导致错误的来源:
gameMain.cpp:
// STD
#include <string>
#include <IOstream>
#include <map>
// GAME
#include "Global_Includes.h" //Include all SDL libs
#include "Global_Mapping.h" //Include object that tells program what to do when this part is finished
#include "Global_Events.h" //Include object to control events
#include "Global_Screen.h" //Include object to control screen
// LOCAL
#include "gameLevelObject.h" //Include level class
#include "gameInit.h" //Include function for initialisation
// DLL EXPORT
#include "gameMain.h"
mapping game(screen* Display, short int Lvl){
mapping ReturnValue;
level GameMap; //Error: Incomplete type
gameInit(Lvl, Display, &GameMap);
}
gameInit.cpp:
// STD
#include <map>
// GAME
#include "Global_Includes.h" //Include all SDL libs
#include "Global_Screen.h" //Include object to control screen
#include "gameLevelObject.h" //Include level class
#include "gameInit.h" //Include header so compiler knows where gameInit is defined
bool gameInit(short int lvl, screen* Display, level* object){
*object = level::level(lvl); //Error: Incomplete type
return true;
}
gameLevelObject.cpp:
// GAME
#include "Global_Includes.h" //Include all SDL libs
// STD
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
// LOCAL
#include "gameLevelObject.h" //Include header so compiler knows where class level is defined
class level{
SDL_Surface* total_map;
int lives;
int livesMINUS;
int livesPLUS;
int position[2];
std::vector<std::string> map;
std::vector<std::pair<int, int> > energyTILES;
public:
level(short int lvl){\*constructor definition*\}
};
gameLevelObject.h:
#include <vector>
class level;
我在Bjarne Stroustrup编写的“编程语言c ++”一书中读到,这就是你应该包含文件的方式。
问题是:为什么编译器不知道gameLevelObject.cpp中定义了class level
?为什么编译器和gameInit
找不到函数class level
?我使用相同的方法将它们链接到gameMain.cpp