表达式没有计算为常量 - C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33423502/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:16:42  来源:igfitidea点击:

expression did not evaluate to a constant- c++

c++

提问by John

I wrote the following code for converting a decimal number to base2. not the best one probably, but it worked on eclipse. however, when I try to run it on visual studio, I get this error message on line 10 (emphasized): "expression did not evaluate to a constant". Why is that?

我编写了以下代码来将十进制数转换为 base2。可能不是最好的,但它适用于日食。但是,当我尝试在 Visual Studio 上运行它时,我在第 10 行(强调)收到此错误消息:“表达式未计算为常量”。这是为什么?

long base2(int number) {
    int remainder, sizeOfRetNum, isNegative = 0;
    if (number<0)
        isNegative = 1;
    int temp = number;
    while (temp != 0) {
        sizeOfRetNum++;
        temp = temp / 2;
    }
    char ansString[sizeOfRetNum]; // ********line 10********
    int j = sizeOfRetNum - 1;
    while (number != 0) {
        remainder = number % 2;
        number = number / 2;
        if (remainder == 0)
            ansString[j] = '0';
        else
            ansString[j] = '1';
        j--;
    }
    long ansNum = atol(ansString);
    if (isNegative == 1)
        ansNum = -ansNum;
    return ansNum;
}

回答by NathanOliver

char ansString[sizeOfRetNum]; 

Is a Variable Length Arrayand is not standard in C++. Some compilers like GCC allow them as an extensions but MSVS will not compile them.

是一个可变长度数组,在 C++ 中不是标准的。一些像 GCC 这样的编译器允许它们作为扩展,但 MSVS 不会编译它们。

In order to get a dynamic array you will need to use a pointer and new

为了获得动态数组,您需要使用指针和 new

char* ansString = new char[sizeOfRetNum];

Or better yet, rework the function to use a std::string, which handles the memory management for you.

或者更好的是,重新编写函数以使用 a std::string,它为您处理内存管理。

回答by E. Moffat

sizeOfRetNumis not a constant value - in other words, its value is not known at compile time.

sizeOfRetNum不是一个常数值——换句话说,它的值在编译时是未知的。

When you want to allocate memory and don't know the value until run time, you need to use dynamic memory allocation. This is done in C++ with operator new. The memory you allocate yourself with newalso needs to be freed with deleteor delete[].

当你想分配内存并且直到运行时才知道值时,你需要使用动态内存分配。这是在 C++ 中使用operator new. 您自己分配的内存new也需要使用delete或释放delete[]

Change char ansString[sizeOfRetNum];to char * ansString = new char[sizeOfRetNum];. Don't forget to call delete [] ansString;before the function returns, or you will have a memory leak.

更改char ansString[sizeOfRetNum];char * ansString = new char[sizeOfRetNum];。不要忘记delete [] ansString;在函数返回之前调用,否则会出现内存泄漏。

回答by Amaresh

Well above solutions will work fine with char type. It won't if you had different type e.g. double or float or any other user-defined type for example

以上解决方案适用于 char 类型。如果您有不同的类型,例如 double 或 float 或任何其他用户定义的类型,则不会

double sample_float_array (n+1) //suppose n is a number passed in the functions

to get done what you intended to, and so to be compiled in MSVS you might need to write like below

为了完成你想要的,所以要在 MSVS 中编译,你可能需要像下面这样写

std::vector <double> sample_float_array;
sample_float_array.resize (n+1);

hope this helps. Cheers

希望这可以帮助。干杯

回答by com

you could use malloc instead of new if you want to use it for C implementation. If you don't you could propably use std::string as @NathanOliver pointed out.

如果您想将它用于 C 实现,您可以使用 malloc 而不是 new。如果你不这样做,你可以像@NathanOliver 指出的那样使用 std::string 。

char* ansString; // ********line 10********

字符* ansString; // ********第 10 行********

ansString = (char*)malloc(sizeOfRetNum*sizeof(char)); // ********line 11********

ansString = (char*)malloc(sizeOfRetNum*sizeof(char)); // ********第 11 行********