Daoud Daoud -4 years ago 138
C++ Question

C++ Declaring a 2d array with a size function

I'm trying to declare a 2d array by using the size() method for STL map:

const int size = edge_map.size();//get row and column size

int a[size][size];//nxn matrix

I keep getting a compiler error that the size must be a constant value even though I am declaring it as a constant. Is there any work around for this without having to use a dynamic 2d array?

Answer Source

const means to not change its original (initial) value.

But size must be known at compile time, as the compiler/linker allocates memory for non-local variables (declared out of any function).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download