Reputation: 11
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?
Upvotes: 1
Views: 70
Reputation: 73542
This is a little confusing due to the nested class, but it's as simple as:
Test::Class1 Test::one(0);
The following statement in your code, just defines a global objet one
that happens to be of a nested type: Test::Class1
:
Test::Class1 one(0);
If it wasn't of a nested type, you'd immediately have noticed the missing qualifier ;-)
Upvotes: 1