Simple code that won't compile

Deewiant deewiant.doesnotlike.spam at gmail.com
Sat Jul 7 12:45:52 PDT 2007


ginophi wrote:
> My very simple test code
> 
> void main()
> {
>     struct test
>     {
>         int a;
>     }
> 
>     int b = 1;
>     test name[b];
> }
> 
> doesn't compile and gives these errors:
> 
> test.d(9): Error: Integer constant expression expected instead of b
> test.d(9): Error: Integer constant expression expected instead of
> cast(uint)b
> test.d(9): Error: Integer constant expression expected instead of
> cast(uint)b
> test.d(9): Error: Integer constant expression expected instead of
> cast(uint)b
> 
> What am I doing wrong?
> 

You're not reading the error message: "Integer constant expression expected
instead of b". b isn't constant, hence it doesn't work.

Either make b constant ("const int b = 1"), or use a dynamic array:

auto name = new test[b];

-- 
Remove ".doesnotlike.spam" from the mail address.


More information about the Digitalmars-d-learn mailing list