0

In the public section of my class declaration, I have this:

static float m_screenWidth;
static float m_screenHeight;

I can then set them to whatever I want in the class constructor or elsewhere, however, the compiler fails when I use them, saying:

Undefined symbols for architecture

This is noted on any line in which I try to access these members. In class methods, I access them by name. In non-member functions I access them with the className:: prefix. Doesn't matter, they are not enjoyed. Any advice?

Worth noting that they are not getting "undeclared" errors, so they are recognized to some degree.

2

2 Answers 2

4

That error message is a linker failure message, not a compiler failure message. It is stating that it cannot find the definitions of the variables.

In the public section they are declarations. They must be defined, exactly once, outside of the class definition:

float className::m_screenWidth;
float className::m_screenHeight;
2

You declared them in the header file. You also have to define them in a .cpp file somewhere:

float MyClass::m_screenWidth;
float MyClass::m_screenHeight;

That will tell the compiler to actually reserve space and create symbols for these static variables.

Not the answer you're looking for? Browse other questions tagged or ask your own question.