gpt4 book ai didi

c++ - 警告 : extended initializer lists only available with -std=c++0x or -std=gnu++0x

转载 作者:搜寻专家 更新时间:2023-10-31 00:31:29 27 4
gpt4 key购买 nike

我尝试声明一个矩阵,当我编译时我得到了这个:

extended initializer lists only available with -std=c++0x or -std=gnu++0x

当尝试另一种解决方案时,我得到了这个:

ISO C++ forbids variable length array 'A' (line 16)

这是我最后一次尝试的代码:

#include<iostream>
#include<cmath>
#include<fstream>
#include<cstdlib>
using namespace std;

int main()
{

int m, l;

ifstream MatrixA ("A.txt");
MatrixA >> m;
MatrixA >> l;

int A [m][l];

for (int lineA = 0; lineA <= m; lineA++)
{
for (int colA = 0; colA <= l; colA++)
{
A [lineA][colA];
}
}

cout << "Matrix A: " << A[m][l] << endl;

return 0;

}

最佳答案

C++ 不支持可变大小的内置数组。如果你需要可变大小的数组维度,你需要使用一些动态地进行必要的内存分配的东西。一个相对直接的替代方法是使用 std::vectors 的 std::vector:

std::vector<std::vector<int> > A(m, std::vector<int>(l));

关于c++ - 警告 : extended initializer lists only available with -std=c++0x or -std=gnu++0x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33974795/

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