英文:
unresolved external symbol "private: static class variable" c++
问题
I'm getting an error while creating a container in c++ with static method and locks.I'm not an expert in c++.Could anyone please help to find the real issue here? below is the code. I'm using v14
#include <iostream>
#include <map>
#include <string>
#include <mutex>
#include <shared_mutex>
class obj {};
class pricercache
{
static std::shared_mutex entry_mutex;
private:
static std::map<std::string,obj>& getInstance()
{
static std::map<std::string, obj> engines;
return engines;
}
public:
pricercache(pricercache const&) = delete;
void operator=(pricercache const&) = delete;
static obj get(std::string const& key)
{
std::shared_lock<std::shared_mutex> lk(pricercache::entry_mutex);
std::map<std::string, obj>::const_iterator const it =
getInstance().find(key);
return it->second;
}
static void add(std::string const& key,
obj engine)
{
std::lock_guard<std::shared_mutex> lk(pricercache::entry_mutex);
auto& engines = getInstance();
if (engines.find(key) == engines.end())
engines.insert(std::make_pair(key, engine));
}
};
int main()
{
std::cout << "Hello World!\n";
obj v1;
obj v2;
pricercache::add("1", v1);
pricercache::add("2", v2);
auto A = pricercache::get("1");
auto b = pricercache::get("2");
std::cout << "Done!\n";
}
The error I'm getting is :
error LNK2001: unresolved external symbol "private: static class std::shared_mutex pricercache::entry_mutex" (?entry_mutex@pricercache@@0Vshared_mutex@std@@A)
I would also welcome and really appreciate if you have any other suggestion about the container class.
thanks in advance.
英文:
I'm getting an error while creating a container in c++ with static method and locks.I'm not an expert in c++.Could anyone please help to find the real issue here? below is the code. I'm using v14
#include <iostream>
#include <map>
#include <string>
#include <mutex>
#include <shared_mutex>
class obj {};
class pricercache
{
static std::shared_mutex entry_mutex;
private:
static std::map<std::string,obj>& getInstance()
{
static std::map<std::string, obj> engines;
return engines;
}
public:
pricercache(pricercache const&) = delete;
void operator=(pricercache const&) = delete;
static obj get(std::string const& key)
{
std::shared_lock<std::shared_mutex> lk(pricercache::entry_mutex);
std::map<std::string, obj>::const_iterator const it =
getInstance().find(key);
return it->second;
}
static void add(std::string const& key,
obj engine)
{
std::lock_guard<std::shared_mutex> lk(pricercache::entry_mutex);
auto& engines = getInstance();
if (engines.find(key) == engines.end())
engines.insert(std::make_pair(key, engine));
}
};
int main()
{
std::cout << "Hello World!\n";
obj v1;
obj v2;
pricercache::add("1", v1);
pricercache::add("2", v2);
auto A = pricercache::get("1");
auto b = pricercache::get("2");
std::cout << "Done!\n";
}
The error I'm getting is :
> error LNK2001: unresolved external symbol "private: static class std::shared_mutex pricercache::entry_mutex" (?entry_mutex@pricercache@@0Vshared_mutex@std@@A)
I would also welcome and really appreciate if you have any other suggestion about the container class.
thanks in advance.
答案1
得分: 0
编译器报错是因为您已声明了静态成员但未定义。您应该在代码的某处定义它,如下所示:
std::shared_mutex pricercache::entry_mutex; // entry_mutex的定义
英文:
The compiler complains because you have declared the static member, but not defined. Somewhere in your code, you should define it like this
std::shared_mutex pricercache::entry_mutex; // definition of the entry_mutex
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论