2D全局数组错误-数组界限不是整数常量 [英] 2D Global Array Error - Array Bound Is Not An Integer Constant

查看:60
本文介绍了2D全局数组错误-数组界限不是整数常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到这个问题的答案。我意识到数组中使用的整数值在编译时必须是已知的,而我这里所拥有的似乎满足这一标准。如果我使用:

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

然后我得到数组绑定错误,即使pt的值在编译时是已知的。但是,当我使用:

时,该代码被接受
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

我是不是漏掉了什么?如果没有,我可以做些什么来让它接受PTS?

推荐答案

使用

int L = 50;
int pts = L + 2;

Lpts不能用作数组的维度,因为它们不是编译时间常量。使用constexpr限定符让编译器知道它们可以在编译时计算,因此可以用作数组的维度。

constexpr int L = 50;
constexpr int pts = L + 2;

这篇关于2D全局数组错误-数组界限不是整数常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆