我有这个问题,如果我包含某个头文件,我将无法编译。
标题代码:
//#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> }
};
}
来自 visual studio 的完整错误信息:
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 ==========
不幸的是错误消息真的很长。我不知道如何解决这个问题,甚至不知道出了什么问题。还有没有更好的方法通过名称实例化类?
提前感谢您的帮助:)
最佳答案
您的test
对象违反了单一定义规则。
#pragma once
防止定义在相同 编译单元中多次出现(阅读:相同的 *.cpp 文件) ,但不会出现在不同 编译单元中(阅读:不同的 *.cpp 文件)。
解决此问题的一种方法是将对象转换为函数内的静态本地对象,并使其可通过引用访问:
header.h:
// ...
namespace ActorFactory {
// ...
typedef std::map<std::string, Actor*(*)()> class_map;
class_map& test();
}
测试.cpp:
#include "header.h"
namespace ActorFactory {
// ...
class_map& test() {
static class_map test = {
{ "Camera", &createInstance<Camera> }
{ "Mesh", &createInstance<Mesh> };
return test;
}
}
这也是针对所谓的 static initialization order fiasco 的故障安全保证。 ,并为您提供惰性初始化(与这个小对象无关紧要)。
您可以通过使用 new
分配对象并且从不删除它来使整个事情更加安全(面对销毁顺序问题),但该函数仍应返回引用:
namespace ActorFactory {
// ...
class_map& test() {
static class_map* test = new class_map {
{ "Camera", &createInstance<Camera> }
{ "Mesh", &createInstance<Mesh> };
return *test;
}
}
阅读我上面链接的常见问题解答以获取所有详细信息。
在任何情况下,客户端代码都可以通过 test()
而不是 test
访问对象。
关于c++ - 错误 LINK2005 已在 Main.obj 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29199726/