gpt4 book ai didi

c++ - 分配非 const 变量的只读位置

转载 作者:行者123 更新时间:2023-11-27 22:36:56 25 4
gpt4 key购买 nike

为什么下面的宏会失败

#define XML_TEST(K, V) K = V;

struct teststruct {
teststruct() {
TEST("x_", 10);
}
int x_;
};

编译时不会出现以下错误:

test.h:30: error: assignment of read-only location ‘"x_"’
#define XML_TEST(K, V) K = V;);

简单地做

teststruct() {
x_ = 10;
} ^

工作正常。我也没有看到这里有任何常量。有谁知道我在这里缺少什么?

最佳答案

TEST("x_", 10);

将处理成

"x_" = 10;

这绝对是个问题,因为我们要分配给常量字符串。我相信你的意思是

TEST(x_, 10);

关于c++ - 分配非 const 变量的只读位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53074668/

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