gpt4 book ai didi

C++ 具有类内初始化程序的成员必须是 const

转载 作者:IT老高 更新时间:2023-10-28 21:56:33 25 4
gpt4 key购买 nike

我正在尝试在我的类中创建一个静态字符串:(在我的头文件中)

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

我做错了什么?

最佳答案

您可以做的是在 header 中声明字符串并在您的 .cpp 中对其进行初始化。

在 MyClass.h 中

#include <string>
class MyClass
{
static std::string foo;
}

在 MyClass.cpp 中

#include "MyClass.h"
std::string MyClass::foo = "bar"

关于C++ 具有类内初始化程序的成员必须是 const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13274876/

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