You don't say what compiler/OS you are using, but since this above snippet is accepted by gcc, I am guessing that you are using MSVC, which does not support variable length arrays. You will have to use dynamic memory allocation to create an appropriately sized array. With modern C++, that is normally done using
std::unique_ptr
#include <memory>
void merge(DataType theArray[], int first, int mid, int last)
{
int n1 = mid - first + 1;
int n2 = last - mid;
auto L1 = std::unique_ptr<DataType[]>(new DataType[n1]);
auto R1 = std::unique_ptr<DataType[]>(new DataType[n2]);
}
Note that in your code,
L1, R1
have types
int
but
theArray
has type
DataType
. Unless DataType is an numeric type (int, float, etc), then the assigments in the loops should be generating errors as well.