Kunal Vyas
Kunal Vyas

Reputation: 1579

cannot appear in a constant-expression

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?

Upvotes: 4

Views: 2913

Answers (2)

Alex
Alex

Reputation: 2372

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.

Upvotes: 4

ralphtheninja
ralphtheninja

Reputation: 133008

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));

Upvotes: 3

Related Questions