0

I have been using Codeblocks and my code was fine. When I switched to Visual Studio I got an error that says : "expression must have constant value"

int board[n][m];

1 Answers1

0

In contrast to in C there is no variable length array (VLA) in C++ standard, i.e. it is implementation/evnvironemt depending whether you can use them with a compiler or not.
You got lucky in CodeBlocks and unlucky in Visual Studio.
Both will allow you to use containers in C++ (e.g. std::vector), which is the recommended way of achieving what you need in C++.

According to OPs comment, teacher explicitly wanted an array used.
Most likely what the mean is the container class std::array, see https://en.cppreference.com/w/cpp/container/array .

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Well... Because of my teacher told me to use arrays and not vectors, I probably should use CodeBlocks then? –  Dec 12 '20 at 13:01
  • There is a C++ container "array", that is most likely what your teacher meant. Check back with them. If they mean to use C-style VLAs in C++, then ... well finding a new teacher is probably not an option for you... Then yes, use CodeBlocks (or the compiler you used there), keep in mind that the teachers advice is questionable and try to get the credits for the class. Then read a book on C++. Not necessarily in that order... – Yunnosch Dec 12 '20 at 13:03
  • Okaay! Thanks for the quick answer! –  Dec 12 '20 at 13:08
  • Your teacher could have wanted you to use dynamic arrays using new[] and delete[]. Many academic institutions still teach mostly c with classes in the first c++ course. – drescherjm Dec 12 '20 at 13:56