C ++类静态成员初始化

时间:2013-12-23 19:32:05

标签: c++ oop static

我有以下代码:

class employee {
public:
    static int last_id;
    ...

};

 int main() {
    employee::last_id=0;
 }

当我尝试运行它时会出现以下错误:

Undefined symbols for architecture x86_64:
  "employee::last_id", referenced from:
      _main in chap7-F3IpS1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.3s with exit code 1]

2 个答案:

答案 0 :(得分:3)

int employee::last_id=0;
int main() {
    [...]

}

答案 1 :(得分:3)

您只声明了静态数据成员但未定义它。在全局命名空间中写在main之前

int employee ::last_id;

虽然你可以明确地指定初始化器,但它将被初始化为零。