静态结构链接器错误

时间:2009-12-05 01:47:56

标签: c++ static struct

我正在尝试在C ++中创建一个静态结构:

static struct Brushes
{
  static HBRUSH white ;
  static HBRUSH yellow ;
} ;

但它不起作用,我得到了:

Error 4 error LNK2001: unresolved external symbol "public: static struct HBRUSH__ * Brushes::white"

为什么?

我们的想法是能够在整个程序中使用Brushes::whiteBrushes::yellow,而无需创建Brushes的实例。

3 个答案:

答案 0 :(得分:6)

您必须在某处定义静态成员,通常在.cxx文件中,例如:

HBRUSH Brushes::white;

原因是头文件没有定义,它只声明它。

答案 1 :(得分:6)

您应该从static行中删除第一个struct Brushes。然后,您需要在.cpp文件中定义初始值(并声明其内存),如下所示:

HBRUSH Brushes::white(some_init_value);
HBRUSH Brushes::yellow(some_init_value);

答案 2 :(得分:2)

所以你需要:

HBRUSH Brushes::white = xxxx;

在您的某个源文件中的某处。并摆脱那个初始的静态。

你知道Win32 GDI中的库存对象,对吗?