Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用类型特征时如何排列文件? 我尝试过C++中的第一个特性,但我正在为多个定义的符号链接错误。 error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK1169: one or more multiply defined symbols found_C++_Templates_Include_Typetraits - Fatal编程技术网

使用类型特征时如何排列文件? 我尝试过C++中的第一个特性,但我正在为多个定义的符号链接错误。 error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK1169: one or more multiply defined symbols found

使用类型特征时如何排列文件? 我尝试过C++中的第一个特性,但我正在为多个定义的符号链接错误。 error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK2005: "public: static class std::unordered_map<std::string, std::string> const ManagerTrait<struct Specialized>::Fields" error LNK1169: one or more multiply defined symbols found,c++,templates,include,typetraits,C++,Templates,Include,Typetraits,专门的 #pragma once #include "manager.h" class Specialized { // ... }; // would like to move in a dedicated specialized/trait.h template <> struct ManagerTrait<Specialized> { // define unordered map static const unordered_map&l

专门的

#pragma once
#include "manager.h"

class Specialized
{
    // ...
};

// would like to move in a dedicated specialized/trait.h
template <>
struct ManagerTrait<Specialized>
{
    // define unordered map
    static const unordered_map<string, string> Fields;

    // define function
    static void Function(Specialized *Instance, Manager::Parameter *Parameter)
    {
        // ...
    }
};

// populate unordered map
const unordered_map<string, string> ManagerTrait<Specialized>::Fields = []{
    unordered_map<string, string> fields;
    // insert some elements
    // ...
    return fields;
}();
#pragma一次
#包括“manager.h”
班级专业
{
// ...
};
//想进入一个专门的专业/特质。h
模板
结构管理器
{
//定义无序映射
静态常量无序映射字段;
//定义函数
静态void函数(专用*实例,管理器::参数*参数)
{
// ...
}
};
//填充无序地图
常量无序映射管理器trait::Fields=[]{
无序的地图字段;
//插入一些元素
// ...
返回字段;
}();
(我删除了命名空间
std::
的出现,以使代码更具可读性。)


如何组织代码文件和包含以使其正常工作?

不要在标题中定义静态成员。您将在每个TU中引入定义,其中
#包括
标题


而是在一个TU中定义它们;最简单的方法是使用.cpp文件。

提供链接错误,请考虑使用引用而不是pointers@Manu343726我添加了链接器错误。此外,我在示例源代码中添加了trait的std::unordered_map成员,因为它可能与问题有关。谢谢,这解决了我的问题。无论如何,在我的示例中,我还有什么其他选项可以初始化
字段
?我想把它放在trait-definition头中的某个地方。在C++11中,如果你稍微改变一下,你可能可以利用内联初始化器。你说得对,VS 12刚刚发布。希望他们能支持这一点。
#pragma once
#include "manager.h"

class Specialized
{
    // ...
};

// would like to move in a dedicated specialized/trait.h
template <>
struct ManagerTrait<Specialized>
{
    // define unordered map
    static const unordered_map<string, string> Fields;

    // define function
    static void Function(Specialized *Instance, Manager::Parameter *Parameter)
    {
        // ...
    }
};

// populate unordered map
const unordered_map<string, string> ManagerTrait<Specialized>::Fields = []{
    unordered_map<string, string> fields;
    // insert some elements
    // ...
    return fields;
}();