Can’t set value of static object field (error LNK2001: unresolved external symbol)

As from your comment

but what is the rule that defines that?

From the c++ reference it says

Definitions and ODR

Definitions are declarations that fully define the entity introduced by the declaration. Every declaration is a definition, except for the following:

  1. Declaration of a static data member inside a class definition
struct S {    // defines S
    int n;        // defines S::n
    static int i; // declares, but doesn't define S::i
};
int S::i = 0; // defines and initializes S::i

As an additional reference you can also check here Wikipedia, One Definition Rule


I finally found the current (2nd June 2014) latest freely available standard reference (a copy of the currently released standard is available for about 30$ I think):

§ 9.4.2

2 The declaration of a static data member in its class definition is not a definition and may be of an incomplete
type other than cv-qualified void. The definition for a static data member shall appear in a namespace
scope enclosing the member’s class definition. In the definition at namespace scope, the name of the static
data member shall be qualified by its class name using the :: operator. The initializer expression in the
definition of a static data member is in the scope of its class

Leave a Comment