Reputation: 603
I can't seem to find an answer to this. I realize that the integer value used in an array must be known at compile time, and what I have here seems to meet that criteria. If I use:
int L = 50; // number of interior points in x and y
int pts = L + 2; // interior points + boundary points
double u[pts][pts], // potential to be found
u_new[pts][pts]; // new potential after each step
Then I get the array bound error, even though the value for pts is known at compile time. The code is accepted, however, when I use:
int L = 50; // number of interior points in x and y
int pts = L + 2; // interior points + boundary points
double u[52][52], // potential to be found
u_new[52][52]; // new potential after each step
Am I missing something here? If not, what can I do to have it accept pts?
Upvotes: 2
Views: 62
Reputation: 206577
When you use
int L = 50;
int pts = L + 2;
L
and pts
cannot be used as dimensions of arrays since they are not compile time constants. Use constexpr
qualifiers to let the compiler know that they can be computed at compile time, and hence can be used as dimensions of arrays.
constexpr int L = 50;
constexpr int pts = L + 2;
Upvotes: 3