[英]Static struct linker error
I'm trying to create a static struct in C++: 我正在尝试在C ++中创建一个静态结构:
static struct Brushes { static HBRUSH white ; static HBRUSH yellow ; } ;
But its not working, I'm getting: 但它不起作用,我得到:
Error 4 error LNK2001: unresolved external symbol "public: static struct HBRUSH__ * Brushes::white"
Why? 为什么?
The idea is to be able to use Brushes::white
, Brushes::yellow
throughout the program, without having to create an instance of Brushes
. 我的想法是能够在整个程序中使用Brushes::white
, Brushes::yellow
,而无需创建Brushes
实例。
You have to define the static members somewhere, usually in the .cxx file, eg: 您必须在某处定义静态成员,通常在.cxx文件中,例如:
HBRUSH Brushes::white;
The reason is that the header file doesn't make the definition, it only declares it. 原因是头文件没有定义,它只声明它。
You should remove the first static
from the struct Brushes
line. 您应该从struct Brushes
行中删除第一个static
。 Then you will need to define the initial values (and declare their memory) in a .cpp file as following: 然后,您需要在.cpp文件中定义初始值(并声明其内存),如下所示:
HBRUSH Brushes::white(some_init_value);
HBRUSH Brushes::yellow(some_init_value);
So you need: 所以你需要:
HBRUSH Brushes::white = xxxx;
somewhere in one of your source files. 在你的一个源文件中的某个地方。 And get rid of that initial static. 并摆脱那个初始的静态。
you do know about the stock objects in Win32 GDI, right? 你知道Win32 GDI中的库存对象,对吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.