“未解决的外部符号“private: static class variable” c++”

huangapple go评论62阅读模式
英文:

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 &lt;iostream&gt;
#include &lt;map&gt;
#include &lt;string&gt;
#include &lt;mutex&gt;
#include &lt;shared_mutex&gt;

class obj {};

class pricercache
{
	static  std::shared_mutex entry_mutex;
private:
	static std::map&lt;std::string,obj&gt;&amp; getInstance()
	{
		static std::map&lt;std::string, obj&gt;    engines;
		return engines;
	}
public:

	pricercache(pricercache const&amp;) = delete;
	void operator=(pricercache const&amp;) = delete;

	static obj get(std::string const&amp; key)
	{
		std::shared_lock&lt;std::shared_mutex&gt; lk(pricercache::entry_mutex);
		std::map&lt;std::string, obj&gt;::const_iterator const it =
			getInstance().find(key);		
		return it-&gt;second;
	}
	static void add(std::string const&amp; key,
		obj engine)
	{
		std::lock_guard&lt;std::shared_mutex&gt; lk(pricercache::entry_mutex);
		auto&amp; engines = getInstance();
		if (engines.find(key) == engines.end())
			engines.insert(std::make_pair(key, engine));
	}
};

int main()
{
    std::cout &lt;&lt; &quot;Hello World!\n&quot;;
	obj v1;	
	obj v2;
	pricercache::add(&quot;1&quot;, v1); 
	pricercache::add(&quot;2&quot;, v2);

	auto A = pricercache::get(&quot;1&quot;);
	auto b = pricercache::get(&quot;2&quot;);

    std::cout &lt;&lt; &quot;Done!\n&quot;;

    
}

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 

huangapple
  • 本文由 发表于 2023年5月11日 02:31:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定