gpt4 book ai didi

c++ - 数组声明的编译器错误?

转载 作者:行者123 更新时间:2023-11-28 02:59:34 25 4
gpt4 key购买 nike

我该如何解决这三个错误?

  • 错误 C2057:需要常量表达式
  • error C2466: 无法分配常量大小为 0 的数组
  • 错误 C2133:“randomTickets”:大小未知

有问题且不喜欢 [tickets] 的线路

int randomTickets[tickets][SIZE];

//global constants
const int SIZE = 6; //This is the number of balls per ticket
const int MAX_WHITE = 58; //This is the range of the white balls
const int MAX_RED = 34; //This is the range of the red balls
const int waysToWin = 9;
int* newAr = new int[SIZE];

int main(int argc, const char * argv[])
{
int tickets = displayMenu(); //Welcome screen lets you buy tickets
int spending = tickets * 2; //Charges you for the tickets
int randomTickets[tickets][SIZE];
//code

预先感谢您的帮助!

最佳答案

error C2057: expected constant expression

你不能声明randomTickets之所以这样,是因为需要在编译时知道数组的维度。 tickets不是编译时间常量,因此你有错误。考虑使用 std::vector<T> :

std::vector<std::vector<int>> randomTickets(tickets, std::vector<int>(SIZE));

或者,您可以嵌套 std::arraySIZE是常量并且在编译时已知:

std::vector<std::array<int, SIZE>> randomTickets(tickets);

其他错误通过修复此错误得到解决。

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

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