我想将所有静态变量对象初始化为相同的值。我在 prog1.h
namespace fal {
class read_f{
public:
static std::string ref_content, seq_content;
int read_fasta(int argc, char **argv);
};
}
我尝试在 prog1.cpp
中初始化它们
std::string fal::read_f::ref_content = seq_content = "";
但是我得到 undefined reference 错误
。
当我尝试
std::string fal::read_f::ref_content = "" ;
std::string fal::read_f::seq_content = "";
它工作正常。
如何在一行中初始化?
最佳答案
您用逗号将它们内联。如果不想重复 fal::
限定符,可以使用 using
声明:
using fal::read_f;
std::string read_f::ref_content = "", read_f::seq_content = "";
此外,从 C++17 开始,您可以使这两个变量内联
,以便它们可以在类定义中定义([class.static.data] ).
关于c++ - 将静态变量对象初始化为相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49550086/