johnbakers
johnbakers

Reputation: 24771

Compiler error on static class variable

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.

Upvotes: 0

Views: 180

Answers (2)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

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.

Upvotes: 2

hmjd
hmjd

Reputation: 122011

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;

Upvotes: 4

Related Questions