std :: unordered_map和链接器错误

时间:2012-09-19 22:03:42

标签: c++ dll linker

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

编译器输出:

1>------ Build started: Project: IRPCore, Configuration: Debug Win32 ------
1>  core.cpp
1>     Creating library D:\Repo\Inter Role Play Gamemode\sa-mp-0.2-plugin-sdk\IRP\Debug\IRPCore.lib and object D:\Repo\Inter Role Play Gamemode\sa-mp-0.2-plugin-sdk\IRP\Debug\IRPCore.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:\Repo\Inter Role Play Gamemode\sa-mp-0.2-plugin-sdk\IRP\Debug\IRPCore.dll : fatal error LNK1120: 1 unresolved externals

核心来自core.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 : "";
} 

我应该链接更多的lib吗?

提前致谢。

编辑:核心定义来自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:
};

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:3)

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

的顶部
std::unordered_map<int, item> core::itemCache;

除了

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

已经在.h。