我需要创建一个程序,用户输入所需的数组大小,然后C++代码创建它,然后允许数据进入它。
这在代码块IDE中有效,但在VisualStudioCommunity2015中不起作用
当我将下面的代码放在CodeBlocks版本13.12中时,它可以工作
#include<iostream>
using namespace std;
int main()
{
int count;
cout << "Making the Array" << endl;
cout << "How many elements in the array " << endl;
cin >> count;
int flex_array[count];
for (int i = 0; i < count; i = i + 1)
{
cout << "Enter the " << i << " term " << endl;
cin >> flex_array[i];
}
for (int j = 0; j < count; j = j + 1)
{
cout << "The " << j << " th term has the value " << flex_array[j] << endl;
}
return 0;
}
但是,如果在Visual 2015中输入相同的代码(即14.0.25425版本),则会得到以下错误:
表达式必须具有常量值。
知道为什么会这样吗?
发布于 2016-12-02 01:32:17
C++没有变长阵列。尽管有些编译器实现是作为扩展,但它仍然不是C++语言的标准特性,也不是可移植的。
如果您想要运行时变量长度数组,请使用std::vector
:
std::vector<int> flex_array(count);
https://stackoverflow.com/questions/40928785
复制相似问题