c++ expression must have a constant value

You can use a std::vector

void createSomething(Items &items)
{
    std::vector<int> arr(items.count); // number of items
}

The reason your first method won’t work is that the size of an array must be know at compile time (without using compiler extensions), so you have to use dynamically sized arrays. You can use new to allocate the array yourself

void createSomething(Items &items)
{
    int* arr = new int[items.count]; // number of items

    // also remember to clean up your memory
    delete[] arr;
}

But it is safer and IMHO more helpful to use a std::vector.

Leave a Comment