When I try to run this simple code, it returns a Variable-sized object may not be initialized error. I have no idea why and how to resolve this problem.
int main()
{
int n=0;
n=1;
int a[n]={}, b[n]={};
return 0;
}
You can initialize your array properly with the std::fill_n like:
std::fill_n(a, n, 0);
std::fill_n(b, n, 0);
or use std::vector like:
std::vector<int> a(n);
It will initialize all the elements to 0 by default.
Or, you can have something like:
constexpr size_t n = 10;
int a[n]{};
This will also initialize all the elements to 0.
std::vectorinstead or makenconst.