警告:扩展初始值设定项列表仅适用于-std=c++0x或-std=gnu++0x

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

本文关键字:-std 适用于 c++0x gnu++0x 列表 扩展 警告      更新时间:2023-10-16

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

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));
相关文章: