重复:
C++: undefined reference to static class member
如果我有这样的类/结构
// header file
class Foo
{
public:
static int bar;
int baz;
int adder();
};
// implementation
int Foo::adder()
{
return baz + bar;
}
这不起作用。我收到“未定义的对 `Foo::bar' 的引用”错误。如何在 C++ 中访问静态类变量?
最佳答案
您必须在实现文件中添加以下行:
int Foo::bar = you_initial_value_here;
这是必需的,因此编译器有一个静态变量的位置。
关于c++ - 在 C++ 中访问静态类变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/743203/