我有这个问题,如果我包含某个标题,我将无法编译。
标题的代码:
//#include "tinyxml2.h"
#pragma once
#include "Camera.h"
#include "Controller.h"
#include "Lights.h"
#include "Mesh.h"
namespace ActorFactory {
//using namespace tinyxml2;
template<class T>
Actor* createInstance() {
return new T;
}
typedef std::map<std::string, Actor*(*)()> class_map;
class_map test = {
{ "Camera", &createInstance<Camera> }
//{ "Mesh", &createInstance<Mesh> }
};
}
来自视觉工作室的完整错误消息:
1>------ Build started: Project: ogl_inf251_ca2, Configuration: Debug Win32 ------
1> Main.cpp
1>d:\development\inf251\ogl_inf251_ca2\model_obj.h(26): warning C4005: '_CRT_SECURE_NO_WARNINGS' : macro redefinition
1> command-line arguments : see previous definition of '_CRT_SECURE_NO_WARNINGS'
1> Generating Code...
1> Compiling...
1> Scene.cpp
1>d:\development\inf251\ogl_inf251_ca2\model_obj.h(26): warning C4005: '_CRT_SECURE_NO_WARNINGS' : macro redefinition
1> command-line arguments : see previous definition of '_CRT_SECURE_NO_WARNINGS'
1> Generating Code...
1>Scene.obj : error LNK2005: "class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class Actor * (__cdecl*)(void),struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class Actor * (__cdecl*)(void)> > > ActorFactory::test" (?test@ActorFactory@@3V?$map@V? $basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@P6APAVActor@@XZU? $less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V? $allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V? $allocator@D@2@@std@@P6APAVActor@@XZ@std@@@2@@std@@A) already defined in Main.obj
1>D:\development\inf251\Debug\ogl_inf251_ca2.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
不幸的是,错误消息真的很长。我不知道如何解决这个问题,甚至不知道出了什么问题。还有更好的方法来按名称实例化类吗?
提前感谢您的帮助:)