Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static struct linker error

Tags:

c++

static

struct

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.

like image 744
bobobobo Avatar asked Feb 04 '26 19:02

bobobobo


2 Answers

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.

like image 136
Richard Pennington Avatar answered Feb 07 '26 09:02

Richard Pennington


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);
like image 35
epatel Avatar answered Feb 07 '26 09:02

epatel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!