#include <iomanip>
#include <iostream>
#include <Windows.h>
using namespace std;
template <class T>
void sort(int n, T a[]){
for(i=0;i<n-1;i++){
for(j=i;j<n;j++){
if(a[i] > a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
void main(){
int size;
cout<<" Please input the amount of numbers you would like to sort"<<endl;
cin>>size;
int Amta[size];
for(int i=0; i<size; i++){
cout<<"Please enter the "<<size+1<< "number";
cin>>Amta[i];
}
Sleep(100000);
}
我正在尝试从用户那里获取用户想要输入的数字并将其存储在可变大小中。
但是当我初始化array Amta[size]
时,出现以下编译错误:
表达式必须具有恒定值
和
C2057:预期的常量表达式“编译错误。
声明数组时,不能在方括号之间输入非常数值:
C++ doesn't allow variable length arrays。大小必须为常数。 C99确实支持它,因此如果需要,您可以使用兼容C99的编译器。一些编译器,例如GCC and Clang also support VLA as an extension in C++ mode
答案取决于“编译器”。每种编程语言或代码都只是带有某些特殊语言文本(源程序)的文本文件。正是编译器(或有时是解释器)将我们的特殊语言“翻译”成另一种目标语言。因此,谁来构建此编译器,是由谁可以做什么和不可以做什么负责。因此,在这种情况下,您使用的c ++编译器不支持“以后定义数组大小”。如果使用G ++编译器,则不会出现此错误。