The Const derived from two other Const can't be used as const in Visual Studio!!(modified)

Below is an example for my problem. obviously, num is an constant int value. However, when I compile it with Visual Studio 2010, an error happens. The error is
"error c2057 expected constant expression"

The code can be compiled successfully with gcc.

Who know how can solve this problem in Visual Studio?
1
2
3
4
5
6
7
8
9
10
  const double totalLength = 20.0;
  const double spacing = 2.0;
  const int num = totalLength/spacing;

  int main()
{
  double panel[num];

  return 0;
}
Last edited on
What is the error? Is it perhaps telling you about a narrowing conversion?
The code can be compiled successfully with gcc.

I don't know what code gcc is generating for line 7, because line 7 makes no sense.

The left hand side of the = is fine if you're trying to declare a simple double. On right hand side, it's not clear what you're trying to do. You have a couple of problems.
1) You can't can declare a dimension or subscript (not clear which you intended) to an unnamed variable.
2) You can't assign an (unnamed) array to a simple double (panel).
3) If you're trying to assign an element of an array, then you don't specify the type name, just the array name, however, no array is decalred.
Last edited on
I'm sorry, i gave a wrong code. And now I have corrected it and add the error message.
That "should" work as the compiler can determine the value of num at compile time. A newer C++11 version of Visual Studio may fix that.

Here's a work around. This is somewhat C-ish, but does compile cleanly is VS2008.
1
2
3
4
5
6
7
8
9
#define totalLength  20.0
#define spacing      2.0
const int num = totalLength/spacing;

int main()
{   double panel[num];

    return 0;
}

Topic archived. No new replies allowed.