c++ - Error: Expression must have a constant value, when variable is constant? -


i have following 3 lines of code in c++ program:

cin >> size; const int arrsize = size;  int inboard[arrsize][arrsize]; 

vs express giving me following error: error: expression must have constant value on final line, @ 2 mentions of arrsize. don't understand because declared arrsize const? problem here, , how can avoid it?

you cannot create array on stack using arrsize size because not constant during compile time. value of arrsize depend on size, happens acquire value during run-time.

solutions:

  • use std::vector<std::vector<int> >
  • allocate own 2d array operator new[] (not recommended. seriously.)

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -