2014-01-16 91 views
0

我该如何解决这三个错误?在数组声明上编译错误?

  • 错误C2057:预期常量表达式
  • 错误C2466:不能分配恒定大小0
  • 错误C2133的数组: 'randomTickets':未知尺寸真实遇到的问题

线和不顺心[机票]

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 

在此先感谢您的帮助!

+0

使'票据'一个常数表达式,并确保它的价值e大于零。 –

+1

你能解释一下你不明白的错误信息部分吗? “预期的常量表达式”非常简单。难道你不知道它在哪里? (你似乎知道这一点,因为你将它隔离为“票”。)难道你不知道一个常量表达是什么? –

+0

'std :: vector > randomTickets(票);'(和雷蒙德的评论+1)。 – WhozCraig

回答

2

错误C2057:预期常量表达式

因为该阵列的尺寸需要在编译时已知你不能声明randomTickets那样。 tickets不是编译时间常量,因此您有错误。考虑使用std::vector<T>

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

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

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

其他错误被固定这一解决。

0

变量tickets不是一个常量表达式,这就是为什么。

变化int randomTickets[tickets][SIZE]如下:

Array* randomTickets = new Array[tickets]; 

的功能main之外,声明以下类型:

typedef int Array[SIZE]; 

可以使用可变randomTickets “正常” 2维阵列从该点随着时间的推移,只要别忘了delete[] randomTickets ...