tags:

views:

123

answers:

3

I'm trying to create a static struct in 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.

+4  A: 

You have to define the static members somewhere, usually in the .cxx file, e.g.:

HBRUSH Brushes::white;

The reason is that the header file doesn't make the definition, it only declares it.

Richard Pennington
The definition has to be in one translation unit, but it could get into one through header files as well.
Georg Fritzsche
+6  A: 

You should remove the first static from the struct Brushes line. Then you will need to define the initial values (and declare their memory) in a .cpp file as following:

HBRUSH Brushes::white(some_init_value);
HBRUSH Brushes::yellow(some_init_value);
epatel
If you're using VC++ you can have these declarations go in the header file (instead of .cpp) if you include __declspec(selectany). Pretty cool trick.
asveikau
+2  A: 

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?

bmargulies