. Advertisement .
..3..
. Advertisement .
..4..
I’m building a new program, but when I run it, an error pops up. The error displayed is as follows:
expression must have a constant value
I have tried several workarounds, but they still do not get the desired results. If you have come across this situation and have a solution for the “c++ expression must have a constant value” problem, pls let me know. Here is what I do:
int row = 8;
int col= 8;
int [row][col];
Thanks!
The cause:
I found that you attempted to set size of an array to values which are not constant. However, C++ does not accept these non-constant values, so the error: ”c++ expression must have a constant value” happens.
Solution:
Let’s follow this simple way to fix your error:
UPD: You can actually accomplish this with some compilers. I believe g++ contains this function, but you should not use it because your code will not be movable to the other compilers.
An array such as this must have a constant size. You will need to allocate memory on the heap to create a dynamic array. After you are done, you can also free it with
delete
You must declare const.
Also,
does not even include a variable name.