数组声明时发生编译器错误? [英] Compiler error on array declaration?

查看:56
本文介绍了数组声明时发生编译器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I go about fixing these three errors?

  • 错误C2057:期望的常量表达式
  • 错误C2466:无法分配恒定大小为0的数组
  • 错误C2133:"randomTickets":大小未知

出现问题且不喜欢[门票]的行

Line that's having the issue and not liking [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

提前感谢您的帮助!

推荐答案

错误C2057:期望的常量表达式

error C2057: expected constant expression

您不能像这样声明 randomTickets ,因为需要在编译时知道数组的尺寸. tickets 不是一个编译时间常数,因此会出现错误.考虑使用 std :: vector< T> :

You can't declare randomTickets like that because the dimensions of the array need to be known at compile time. tickets is not a compile time constant and therefore you have the error. Consider using std::vector<T>:

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

或者,您可以嵌套 std :: array ,因为 SIZE 是恒定的,并且在编译时就知道:

Alternatively, you can nest a std::array since SIZE is constant and known at compile time:

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

通过修复此错误,可以解决其他错误.

The other errors are resolved by fixing this one.

这篇关于数组声明时发生编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆