variable-sized object may not be initialized c++

You can declare an array only with constant size, which can be deduced at compile time. zo1,zo2 and zoA are variables, and the values can be known only at runtime.

To elaborate, when you allocate memory on the stack, the size must be known at compile time. Since the arrays are local to the method, they will be placed on the stack. You can either use constant value, or allocate memory in the heap using new, and deallocate when done using delete, like

int* zod1 = new int[zo1];
//.... other code


delete[] zod1;

But you can use vector instead of array here also, and vector will take care of allocation on the heap.

As a side note, you should not pass vector by value, as the whole vector will be copied and passed as argument, and no change will be visible at the caller side. Use vector<char>& zodis1 instead.

Leave a Comment