4

In the following c++ program:

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. why am I getting this problem since i have put an integer cast in front? How should i fix this?

Kunal Vyas
  • 1,499
  • 1
  • 23
  • 40

2 Answers2

4

The constant-expression that the compiler is referring to is actually the bounds of the array 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.

Alex
  • 2,152
  • 2
  • 17
  • 20
3

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)

static const int row = static_cast<int>(log(BHR_LEN*G_PHT_COUNT)/log(2));
rtn
  • 127,556
  • 20
  • 111
  • 121
  • 1
    Shouldn't use C style casts in C++. This also won't fix the problem. – Andrew Rasmussen May 08 '11 at 19:41
  • This "answer" doesn't answer the question of why an error is being raised. Alex's answer correctly identifies that the **logically** const value of row is not a **constant expression** and therefore cannot be used as the size of an array. The error may be reported on the wrong line, but this "answer" doesn't provide a solution. – monkey0506 Mar 18 '15 at 17:27