我有多个内存数据字典,我必须跟踪,所以我试图在DLL的include文件中设置一个包含以下代码的接口:
LinkData.hpp
#pragma once
#include <string>
#include <map>
#ifdef LINKDATA_EXPORTS
#define API /*__declspec(dllexport) */
#else
#define API /*__declspec(dllimport) */
#endif
template <typename T>
class LinkDataFactory
{
public:
static API T Get(std::string key)
{
return _data[key];
}
template <typename T>
static API void Set(std::string key, T value)
{
_data[key] = value;
}
private:
API static std::map<std::string, T > _data;
};
API宏未定义,因为我理解模板类位于包含文件中,并将在编译时实例化。
我尝试使用带有以下代码的静态类:
#include "LinkData.hpp"
//...
LinkDataFactory<std::string>::Set(key, data);
编译失败,错误:
error LNK2001: unresolved external symbol
"public: static class
std::map<
class std::basic_string<char,struct std::char_traits<char>, class std::allocator<char> >,
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,
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 std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > >
LinkDataFactory<
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >
定义API宏会出错:
error C2491: 'LinkDataFactory<T>::Set' : definition of dllimport function not allowed
发生了什么?我甚至可能尝试过什么?