gpt4 book ai didi

c++ - 当整数变量用于在 C++ 中声明数组大小时,错误显示为 "Expression must have a const value"

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

你好,我有下面的代码,

# include <iostream>
# include <limits>

using namespace std;

int main()
{
int runs,innings,timesnotout,n;
cout<<"Enter the number of players:";
cin>>n;
const char name[n][10]; //here error shows as "Expression must have a constant value" is displayed


}

我试图从输入中获取 n 值,然后使用 n 值作为数组大小

最佳答案

这正是错误消息所说的意思。您只能使用常量表达式来声明数组。在您的情况下,最好使用 std::vector<std::string>甚至 std::vector<std::array<char, 10>>` 或在堆中分配数组。

关于c++ - 当整数变量用于在 C++ 中声明数组大小时,错误显示为 "Expression must have a const value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20977330/

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