简体   繁体   English

不能出现在常量表达式中

[英]cannot appear in a constant-expression

In the following c++ program: 在以下c ++程序中:

static const int row = (dynamic_cast<int>(log(BHR_LEN*G_PHT_COUNT)/log(2)));
static const int pht_bits = ((32*1024)/(G_PHT_COUNT * G_PHT_COUNT * BHR_LEN));
unsigned char tab[pht_bits][1<<row];

I get the error message double log(double)' cannot appear in a constant-expression . 我得到错误消息double log(double)'不能出现在常量表达式中 why am I getting this problem since i have put an integer cast in front? 为什么我得到这个问题,因为我已经在前面放了一个整数? How should i fix this? 我该如何解决这个问题?

The constant-expression that the compiler is referring to is actually the bounds of the array tab . 编译器引用的常量表达式实际上是数组tab的边界。 The dimensions of statically allocated arrays have to be known at compile-time, but the value of row can't be determined until runtime, because it is evaluated using a function. 静态分配的数组的维度必须在编译时知道,但是row的值直到运行时才能确定,因为它是使用函数计算的。

To you that are downvoting my answer. 对你来说,我的回答是低估的。 Tell me that this code does not work: 告诉我这段代码不起作用:

#include <stdio.h>

double log(double foo)
{
  return 1.0;
}

static const int row = static_cast<int>(log(4)/log(2));

int main(void)
{
  printf("%d\n", row);
  return 0;
}

Original (changed from (int) to static_cast, not that it matters) 原始(从(int)更改为static_cast,而不是重要)

static const int row = static_cast<int>(log(BHR_LEN*G_PHT_COUNT)/log(2));

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

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