C++ 表达式必须具有常量值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19556181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Expression must have constant value
提问by G V
#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);
}
I am trying to get the how many numbers the user would like to input from the user and store it in the variable size.
我正在尝试获取用户想要从用户输入的数字数量并将其存储在可变大小中。
But when I initialize array Amta[size]
I get the following compile errors:
但是当我初始化时,array Amta[size]
我收到以下编译错误:
Expression must have constant value
表达式必须具有常量值
and
和
C2057: expected constant expression" compile error.
C2057:预期的常量表达式”编译错误。
回答by Aaron Golden
You can't enter a non-constant value between the brackets when you declare your array:
声明数组时,不能在括号之间输入非常量值:
int Amta[size];
Since you're getting size
from the user, the compiler can't tell ahead of time how much memory it needs for Amta
. The easiest thing to do here (especially for an exercise) is to just choose a relatively large value and make that the constant allocation, like:
由于您是size
从用户那里获得的,编译器无法提前知道它需要多少内存Amta
。在这里做的最简单的事情(特别是对于练习)是只选择一个相对较大的值并使其成为常量分配,例如:
int Amta[1024];
And then if you want to be careful (and you should) you can check if (size > 1024)
and print an error if the user wants a size that's beyond the pre-allocated bounds.
然后,如果您想小心(并且您应该)if (size > 1024)
,如果用户想要超出预先分配范围的大小,您可以检查并打印错误。
If you want to get fancy, you can define Amta
with no pre-set size, like int *Amta;
and then you allocate it later with malloc
:
如果你想要花哨,你可以Amta
不使用预设大小来定义,int *Amta;
然后你稍后用malloc
:
Amta = (int *)malloc(sizeof(int) * size);
Then you must also free Amta
later, when you're done with it:
然后你也必须Amta
稍后释放,当你完成它时:
free(Amta);
回答by phuclv
C++ doesn't allow variable length arrays. The size must be a constant. C99 does support it so if you need you can use a C99 compliant compiler. Some compilers like GCC and Clang also support VLA as an extension in C++ mode
C++ 不允许变长数组。大小必须是常数。C99 确实支持它,因此如果您需要,可以使用符合 C99 的编译器。一些编译器如GCC 和 Clang 也支持 VLA 作为 C++ 模式下的扩展
But if C++ is a must then you can use alloca
(or _alloca
on Windows) to allocate memory on stack and mimic the C99 variable length array behavior
但是如果 C++ 是必须的,那么你可以使用alloca
(或_alloca
在 Windows 上)在堆栈上分配内存并模仿 C99 可变长度数组行为
Amta = (int *)alloca(sizeof(int) * size);
This way you don't need to free the memory after going out of scope because the stackframe will automatically be restored. However you need to be very careful while using this. It's still better to use std::vector
in C++ for these purposes
这样你就不需要在超出范围后释放内存,因为堆栈帧会自动恢复。但是,您在使用它时需要非常小心。std::vector
出于这些目的,在 C++ 中使用仍然更好