I've been looking for an answer to this for several hours. Even though I have a workaround I'd like to understand the problem. I get an undefined reference error when linking the following:
.h:
class Test
{
public:
class Class1
{
public:
Class1(int i);
int x;
void Inc();
};
static Class1 one;
static int F1();
};
.cpp:
#include "Test.h"
Test::Class1 one(0);
void Test::Class1::Inc()
{
x++;
}
Test::Class1::Class1(int i)
{
x = i;
}
int Test::F1()
{
//extern Test::Class1 one;
one.Inc();
return one.x;
}
I get an undefined reference to Test:one in the F1 function. If I add the extern Test::Class1 one it seems to work. Why is this necessary?