std::unordered_map 和链接器错误

std::unordered_map and linker errors

本文关键字:链接 错误 map unordered std      更新时间:2023-10-16

我在编译新项目时遇到了一些问题。

编译器输出:

1>------ Build started: Project: IRPCore, Configuration: Debug Win32 ------
1>  core.cpp
1>     Creating library D:RepoInter Role Play Gamemodesa-mp-0.2-plugin-sdkIRPDebugIRPCore.lib and object D:RepoInter Role Play Gamemodesa-mp-0.2-plugin-sdkIRPDebugIRPCore.exp
1>core.obj : error LNK2001: unresolved external symbol "public: static class std::tr1::unordered_map<int,struct item,class std::hash<int>,struct std::equal_to<int>,class std::allocator<struct std::pair<int const ,struct item> > > core::itemCache" (?itemCache@core@@2V?$unordered_map@HUitem@@V?$hash@H@std@@U?$equal_to@H@3@V?$allocator@U?$pair@$$CBHUitem@@@std@@@3@@tr1@std@@A)
1>main.obj : error LNK2001: unresolved external symbol "public: static class std::tr1::unordered_map<int,struct item,class std::hash<int>,struct std::equal_to<int>,class std::allocator<struct std::pair<int const ,struct item> > > core::itemCache" (?itemCache@core@@2V?$unordered_map@HUitem@@V?$hash@H@std@@U?$equal_to@H@3@V?$allocator@U?$pair@$$CBHUitem@@@std@@@3@@tr1@std@@A)
1>D:RepoInter Role Play Gamemodesa-mp-0.2-plugin-sdkIRPDebugIRPCore.dll : fatal error LNK1120: 1 unresolved externals

核心来了.cpp

#include <iostream>
#include <unordered_map>
#include "core.h"
core::core(void)
{
}
core::~core(void)
{
}
void core::Item_Insert(int uid, item Item)
{
    core::itemCache.insert(std::make_pair<int,item>(uid, Item));
    return;
}
std::string core::convertNativeStringToString(AMX *amx, cell input)
{
    char *string = NULL;
    amx_StrParam(amx, input, string);
    return string ? string : "";
} 

我应该链接更多的库吗?

提前谢谢。

编辑:这是来自core.h的核心定义

class core
{
    public:
        static std::unordered_map<int, item> itemCache;
        core(void);
        ~core(void);
        static void Item_Insert(int uid, item Item);
        static std::string convertNativeStringToString(AMX *amx, cell input);
    private:
};

core::itemCache的定义添加到 core.cpp。通常,类的静态数据成员在类定义中声明,并在源文件中定义

正如@Pete Becker所说,把它放在cpp的顶部:

std::unordered_map<int, item> core::itemCache;

除了

static std::unordered_map<int, item> itemCache;

已经在 .h.