gpt4 book ai didi

c++ - 初始化静态类成员

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:49:49 25 4
gpt4 key购买 nike

<分区>

我正在尝试初始化一个静态类成员,但没有成功。这是一个测试:

文件测试.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() 将做一些实际工作来设置静态成员。有人可以指出错误吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com