1

How do I go about fixing these three errors?

  • error C2057: expected constant expression
  • error C2466: cannot allocate an array of constant size 0
  • error C2133: 'randomTickets' : unknown size

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

Thanks in advance for your help!

3
  • Make tickets a constant expression and make sure it's value is greater than zero. Commented Jan 16, 2014 at 21:10
  • 1
    Can you explain what part of the error message you don't understand? "Expected constant expression" is pretty straightforward. Is it that you don't know where it was expected? (You seem to know that, since you isolated it to tickets.) Is it that you don't know what a constant expression is? Commented Jan 16, 2014 at 21:11
  • std::vector<std::array<int,SIZE>> randomTickets(tickets); (and +1 for Raymond's comment). Commented Jan 16, 2014 at 22:34

2 Answers 2

3

error C2057: expected constant expression

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));

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Variable tickets is not a constant expression, that's why.

Change int randomTickets[tickets][SIZE] as follows:

Array* randomTickets = new Array[tickets];

Outside of function main, declare the following type:

typedef int Array[SIZE];

You can use variable randomTickets as a "normal" 2-dimensional array from this point onwards, just don't forget to delete[] randomTickets when you're done...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.