gpt4 book ai didi

c++ - 在字符串常量之前预期为 ‘,’ 或 ‘...’

转载 作者:行者123 更新时间:2023-12-02 16:21:38 25 4
gpt4 key购买 nike

为什么 const std::string str="__$HOOK_FUNC_FOR_LUA_KEY@__"; 没问题,而编译器在编译时报错

const std::string str("__$HOOK_FUNC_FOR_LUA_KEY@__");?

注意:str是C++类的成员变量。

演示代码:

class Demo
{
private:
const std::string str("__$HOOK_FUNC_FOR_LUA_KEY@__");
}

错误信息如下:

test.hpp:253:51: error: expected identifier before string constant
const std::string str("__$HOOK_FUNC_FOR_LUA_KEY@__");
^
test.hpp:253:51: error: expected ‘,’ or ‘...’ before string constant

很明显,std::string(char*) 有构造函数。所以我真的很困惑。

最佳答案

您不能在类中内联类似函数的初始化。您必须使用大括号 {} 或“赋值”之类的语法 =

这是一种解决检测您是在声明函数还是变量的问题的方法。

当您处理 std::string 时,它有一个 std::initializer_list 构造函数(将用于大括号初始化),您只能使用类似赋值的语法:

const std::string str = "__$HOOK_FUNC_FOR_LUA_KEY@__";

当然,您也可以使用构造函数初始化列表:

Demo()
: str("__$HOOK_FUNC_FOR_LUA_KEY@__")
{}

关于c++ - 在字符串常量之前预期为 ‘,’ 或 ‘...’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65353809/

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