【发布时间】:2013-08-12 22:03:35
【问题描述】:
MainGame.h
#ifndef MainGame_h
#define MainGame_h
#include <string>
#include <sstream>
#include "Horde3D.h"
//definitions
#endif MainGame_h
MainGame.cpp
#include <math.h>
#include <iomanip>
#include "Horde3DUtils.h"
#include "MainGame.h"
#include "GameConfig.h" //<--
//code
main.cpp
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include "glfw.h"
#include "MainGame.h"
#include "GameConfig.h" //<--
//code
GameConfig.h
#ifndef GameConfig_h
#define GameConfig_h
#include <string>
#include <sstream>
#define MAX_PATH 260
class GameConfig
{
static std::string ExtractStartupPath(char *full_app_path)
{
const std::string s(full_app_path);
if(s.find( "/" ) != std::string::npos)
return s.substr( 0, s.rfind( "/" )) + "/";
else if(s.find( "\\" ) != std::string::npos )
return s.substr( 0, s.rfind( "\\" )) + "\\";
else
return "";
}
public:
static bool IsFullScreen;
static int StatMode;
static int FreezeMode;
static bool DebugViewMode;
static bool WireframeMode;
static char *GameTitle;
static int WindowWidth, WindowHeight;
static char StartupPath[MAX_PATH];
static char ContentPath[MAX_PATH];
static void Initialize(char *startup_path)
{
GameTitle = "TestGame\0";
std::string startup_dir = ExtractStartupPath(startup_path);
memcpy(StartupPath, startup_dir.c_str(), startup_dir.length() * sizeof(char));
std::string path(StartupPath);
path.erase(path.find_last_of('\\'), std::string::npos);
path.append("\\Content");
memcpy(ContentPath, path.c_str(), path.length() * sizeof(char));
}
};
int GameConfig::StatMode = 0;
int GameConfig::FreezeMode = 0;
bool GameConfig::DebugViewMode = false;
bool GameConfig::WireframeMode = false;
bool GameConfig::IsFullScreen = false;
int GameConfig::WindowWidth = 800;
int GameConfig::WindowHeight = 600;
char GameConfig::StartupPath[MAX_PATH] = { 0 };
char GameConfig::ContentPath[MAX_PATH] = { 0 };
char *GameConfig::GameTitle = "TestGame\0";
#endif GameConfig_h
编译时出现链接器错误...
main.obj : error LNK2005: "public: static int GameConfig::StatMode" (?StatMode@GameConfig@@2HA) is already define in в MainGame.obj
但我不明白为什么... GameConfig 只有两个包含 - 一个在 MainGame.cpp 中,第二个在 main.cpp 中。那些不应该越过。就算他们穿越了,那#ifndef GameConfig_h、#define GameConfig_h和#endif GameConfig又是为了什么?
我使用的是 VC++ 2010 速成版
【问题讨论】:
-
能否将
GameConfig类的定义添加到GameConfig.h中的sn-p中? -
完成。查看更新后的问题。
-
感谢所有回答的人。
标签: c++ include-guards