ctd*_*015 1 c++ arrays vector codeblocks visual-studio-2015
我需要创建一个程序,用户输入所需的数组大小,然后C++代码创建它,然后允许数据输入.
这适用于Code Blocks IDE,但不适用于Visual Studio Community 2015
当我在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;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我在Visual Studio 2015中输入相同的代码(即版本14.0.25425),我会收到错误:
表达式必须具有常量值
知道为什么会这样吗?
C++没有可变长度数组.一些编译器实现虽然是一个扩展,但它仍然不是C++语言的标准功能,它不可移植.
如果你想使用运行时变量长度数组std::vector
:
std::vector<int> flex_array(count);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
75 次 |
最近记录: |