我正在尝试初始化一个静态类成员并且没有运气。这是一个测试:
文件测试.h
#include <string>
class Test {
public:
static void init(char*);
private:
static std::string *sp;
};
文件测试.cpp
#include "Test.h"
// Initialize the class
void
Test::init(char *foo) {
Test::sp = new std::string(foo);
}
int main(int argc, char** argv) {
Test::init(argv[1]); // call the class initializer
}
链接器失败并显示:
Undefined symbols for architecture x86_64:
"Test::sp", referenced from:
Test::init(char*) in Test-OK13Ld.o
ld: symbol(s) not found for architecture x86_64
在现实世界中,init() 会做一些实际的工作来设置静态成员。有人可以指出错误吗?