I have seemingly simple and straightforward segment of code that is a simplified version of a problem I have been having in a game I am writing. I am trying to set a static field in one class to another value from my main method. However this code will not and I don't understand why.
I get the error
1>Source.obj : error LNK2001: unresolved external symbol "public: static class A * B::a" (?a@B@@2PAVA@@A)
class A
{
public:
A()
{
}
};
class B
{
public:
static A* a;
};
int main()
{
B::a = new A;
}
What is the rule saying that I have to define my static class member outside the class to get it linked?
As from your comment
From the c++ reference it says
As an additional reference you can also check here Wikipedia, One Definition Rule
UPDATE:
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):
You did not define the static data member. You only declared it in the class definition. Add the following line
before the main.
Or you could initialize it with using operator new.
As for your question
then according to paragraph #2 of section 9.4.2 Static data members of the C++ Standard