C ++由静态数据字段引起的消费者文件中的错误链接 [英] C++ Error linking in consumer file caused by static data field
问题描述
我想使用静态全局变量作为互斥体。当我尝试编译以下代码:
I want to use a static global variable as a mutex. When I try to compile the following code:
//header file
class __declspec(dllexport) StateConservator
{
private:
StateConservator();
StateConservator(const StateConservator&);
protected:
const CString m_oldConf;
CContainer& m_container;
static bool x_mutex;
public:
StateConservator(CContainer& container, const CString& conf)
: m_container(container)
, m_oldConf(!x_mutex? container.GetConf():_T(""))
{
if(!x_mutex)
{
x_mutex= true;
m_container.SetConf(conf);
}
}
~StateConservator()
{
if(x_mutex)
{
x_mutex= false;
m_container.SetConf(m_oldConf);
}
}
};
//cpp file
bool StateConservator::x_mutex= false;
//consumer file
StateConservator cs(*pContainer, pDoc->GetConfiguration());
我得到错误:
code> Consumer.obj:error LNK2001:未解析的外部符号protected:static bool StateConservator :: x_mutex(?x_mutex @ StateConservator @@ 1_NA)
Consumer.obj : error LNK2001: unresolved external symbol "protected: static bool StateConservator::x_mutex" (?x_mutex@StateConservator@@1_NA)
请问我该如何解决问题?
Please, how can I solve the problem?
UPDATE
我创建了两个最小的程序只包含测试问题的关键部分,他们工作!这更奇怪了!
I created two minimal programs containing only the essential part to test the problem and they work! This is getting even more strange!
更新2
请注意
Notice the __declspec(dllexport) declaration after the class, that was missing.
推荐答案
对不起,人们。
我在属于项目的文件中定义了StateConservator的代码。但后,我意识到,这将更有意义在另一个文件。
I was defining the code of StateConservator in a file belonging to the project. But after I realized it would make more sense in another file.
我没有意识到第二个文件是另一个解决方案的一部分。所以,我没有编译第二个解决方案,因此错误。
I was not realizing the second file was part of another solution. So, I did not compile the second solution, hence the error.
我想我需要一个扩展Visual Studio来着色其他项目的选项卡与另一种颜色。
I think I need an extension to Visual Studio to colorize the tabs of other projects with another color. If somebody knows about one, I will appreciate.
感谢您支持我造成的烦恼。
Thanks for supporting the annoyance caused by me.
strong> UPDATE
UPDATE
请注意类后缺少 __ declspec(dllexport)声明。
这篇关于C ++由静态数据字段引起的消费者文件中的错误链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!