How can I edit my code to create a std::array from a size not known at compile time?
You can’t. std::array
is a fixed-sized array, it’s size must be a constant known at compile-time.
To make a dynamic array whose size is not known until runtime, you must use new[]
instead:
int *items = new int[fsize];
...
delete[] items;
Preferably, you should use std::vector
instead, which handles that for you:
std::vector<int> items(fsize);
If you don’t want to use std::vector
, you can use std::unique_ptr<int[]>
instead:
std::unique_ptr<int[]> items(new int[fsize]);
or
auto items = std::make_unique<int[]>(fsize);
solved How to fix compiler error when try to initialise a std::array from a non const size [duplicate]