gpt4 book ai didi

c++ - 二维全局数组错误 - 数组绑定(bind)不是整数常量

转载 作者:搜寻专家 更新时间:2023-10-31 01:33:43 24 4
gpt4 key购买 nike

我似乎找不到这个问题的答案。我意识到数组中使用的整数值必须在编译时已知,而我这里的内容似乎符合该标准。如果我使用:

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

然后我得到数组绑定(bind)错误,即使 pts 的值在编译时已知。然而,当我使用时,代码被接受:

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

我是不是漏掉了什么?如果没有,我该怎么做才能让它接受积分?

最佳答案

当你使用

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

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

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

关于c++ - 二维全局数组错误 - 数组绑定(bind)不是整数常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40922460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com