几个小时以来,我一直在寻找这个问题的答案。即使我有一个解决方法,我也想了解这个问题。链接以下内容时出现未定义的参考错误:
。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;
}
我在 F1 函数中得到一个未定义的对 Test:one 的引用。如果我添加 extern Test::Class1 一个,它似乎可以工作。为什么这是必要的?