Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从用户c+;获取2d数组的值+;_C++_Arrays - Fatal编程技术网

C++ 从用户c+;获取2d数组的值+;

C++ 从用户c+;获取2d数组的值+;,c++,arrays,C++,Arrays,我试图通过从用户获取值来创建二维数组 这是我的代码: #include <iostream> using namespace std; int main() { int a, b; cout << "Please enter first value : "; cin >> a; cout << "Please enter second value : "; cin >> b; int myArray[a][b]; syst

我试图通过从用户获取值来创建二维数组

这是我的代码:

#include <iostream>

using namespace std;

int main()
{

int a, b;

cout << "Please enter first value : ";
cin >> a;
cout << "Please enter second value : ";
cin >> b; 

int myArray[a][b];

system("pause");        
return 0;
}
#包括
使用名称空间std;
int main()
{
INTA,b;
cout>a;
cout>b;
int myArray[a][b];
系统(“暂停”);
返回0;
}
它在int-myArray[a][b]下面画一条红线;上面写着: 错误:表达式必须具有常量值


我几乎搜索了所有没有找到答案的地方。

数组不能使用变量定义,在试图使用
a
b
的地方需要常量


如果需要动态分配,请检查要使用的替代结构。它必须是数组吗?

这样的数组不能动态分配。既然你使用C++,考虑使用<代码> STD::vector < /C> >代替C样式数组。我认为需要有一个MUG来表示使用STD::vector,或者T恤SHILL,我们在问题和答案的去处之间有明显的区别。我已回滚您的编辑,该编辑为问题正文添加了一个答案。@BarmakShemirani
std::array
在这里不相关,因为它也不支持变量(非
constexpr
)大小(最终只是普通数组的包装器)。可能重复