我正在尝试在我的类中创建一个静态字符串:(在我的头文件中)
static string description = "foo";
但我收到这个错误:
IntelliSense: a member with an in-class initializer must be const
如果我把它改为:
static const string description = "foo";
我得到这个错误:
IntelliSense: a member of type "const std::string" cannot have an in-class initializer
我做错了什么?
解决方法
你可以做的是在标题中声明字符串,并在.cpp中初始化它.
在MyClass.h
#include <string> class MyClass { static std::string foo; }
在MyClass.cpp
#include "MyClass.h" std::string MyClass::foo = "bar"