gpt4 book ai didi

c++ - 编译错误

转载 作者:行者123 更新时间:2023-11-28 01:11:00 26 4
gpt4 key购买 nike

我正在尝试编译,但遇到了这些错误:

1>.\item.cpp(123) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)

1>.\commands.cpp(1372) : error C2057: expected constant expression
1>.\commands.cpp(1372) : error C2466: cannot allocate an array of constant size 0
1>.\commands.cpp(1372) : error C2133: 'buffer' : unknown size

第 123 行 item.cpp

if((bool)random_range(0, 1))

第 1372 行命令.cpp

if(money < changeSexPrice)
{
char buffer[70 + changeSexPrice];
sprintf(buffer, "You do not have enough money. You need %d gold coins to change your sex.", changeSexPrice);
player->sendCancel(buffer);
return false;
}

有什么想法吗?

最佳答案

您的问题是 char buffer[70 + changeSexPrice];。 win32 编译器在进行堆栈分配时需要常量表达式。

我不确定您为什么要添加 changeSexPrice,因为您只使用缓冲区来打印一个 int。我敢打赌,如果您选择像 char buffer[1024] 这样的东西,那么您的需求将绰绰有余。

编辑:根据评论(非常好)。

如果您使用 len 1024 的固定大小缓冲区,请使用 snprintf。在 Visual Studio 的例子中,这是 sprintf_s .您的代码将更改为:

sprintf_s(buffer, 1024, "You don't have enough money ...", yourValueHere);

或者,Mark B presents an answer这也消除了您自己分配内存的需要。

关于c++ - 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3264929/

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