简体   繁体   English

C++ 表达式必须具有常量值

[英]C++ Expression must have constant value

#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:预期的常量表达式”编译错误。

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 .由于您从用户那里获取size ,因此编译器无法提前告知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:在这里做的最简单的事情(特别是对于练习)是只选择一个相对较大的值并使其成为常量分配,例如:

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;如果你想获得幻想,你可以定义Amta没有预先设定的尺寸,像int *Amta; and then you allocate it later with malloc :然后你稍后用malloc分配它:

Amta = (int *)malloc(sizeof(int) * size);

Then you must also free Amta later, when you're done with it:然后你还必须稍后释放Amta ,当你完成它时:

free(Amta);

C++ doesn't allow variable length arrays . C++ 不允许变长数组 The size must be a constant.大小必须是常数。 C99 does support it so if you need you can use a C99 compliant compiler. C99 确实支持它,因此如果您需要,可以使用符合 C99 的编译器。 Some compilers like GCC and Clang also support VLA as an extension in C++ mode一些编译器如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 (或Windows 上的_alloca )在堆栈上分配内存并模仿 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出于这些目的,最好在 C++ 中使用std::vector

The answer depends on 'Compiler'. 答案取决于“编译器”。 Every programming language or code is simply a text file with some special language text (the source program). 每种编程语言或代码都只是带有某些特殊语言文本(源程序)的文本文件。 It is the compiler (or sometimes interpreter) that 'translates' our special language into another target language. 正是编译器(或有时是解释器)将我们的特殊语言“翻译”成另一种目标语言。 SO who ever built this compiler is responsible for what can be done and what cannot be done. 因此,谁来构建此编译器,是由谁可以做什么和不可以做什么负责。 So in this case, you are using a c++ compiler that does not support "defining array size later". 因此,在这种情况下,您使用的c ++编译器不支持“以后定义数组大小”。 If you use the G++ compiler, you will not get this error. 如果使用G ++编译器,则不会出现此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM