错误 LINK2005 已在 Main.obj 中定义

Jen*_*ens 0 c++ factory object lnk2005

我遇到这个问题,如果我包含某个标头,则无法编译。

标题的代码:

//#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> }
    };
}
Run Code Online (Sandbox Code Playgroud)

来自 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 ==========
Run Code Online (Sandbox Code Playgroud)

不幸的是,错误消息真的很长。我不知道如何解决这个问题,甚至不知道出了什么问题。还有更好的方法通过名称实例化类吗?

感谢您提前提供任何帮助:)

Chr*_*ckl 5

您的test对象违反了单一定义规则。

这可以防止定义在同一#pragma once编译单元中多次出现(读取:相同的 *.cpp 文件),但不会出现在不同的编译单元中(读取:不同的 *.cpp 文件)。

解决此问题的一种方法是将对象转换为函数内的静态本地对象,并使其可通过引用进行访问:

标头.h:

// ...

namespace ActorFactory {

    // ...

    typedef std::map<std::string, Actor*(*)()> class_map;
    class_map& test();
}
Run Code Online (Sandbox Code Playgroud)

测试.cpp:

#include "header.h"

namespace ActorFactory {

    // ...

    class_map& test() {
        static class_map test = {
            { "Camera", &createInstance<Camera> }
            { "Mesh", &createInstance<Mesh> };
        return test;
    }
}
Run Code Online (Sandbox Code Playgroud)

这也是针对所谓的静态初始化顺序惨败的故障安全保证,并为您提供延迟初始化(对于这个小对象来说这并不重要)。

new您可以通过分配对象并且从不删除它来使整个事情变得更加安全(面对破坏顺序问题) ,但该函数仍然应该返回一个引用

namespace ActorFactory {

    // ...

    class_map& test() {
        static class_map* test = new class_map {
            { "Camera", &createInstance<Camera> }
            { "Mesh", &createInstance<Mesh> };
        return *test;
    }
}
Run Code Online (Sandbox Code Playgroud)

请阅读我上面链接的常见问题解答以获取所有详细信息。

无论如何,客户端代码只需通过而test()不是访问对象test