Thread overview
Simple code that won't compile
Jul 07, 2007
ginophi
Jul 07, 2007
Deewiant
Jul 07, 2007
ginophi
Jul 07, 2007
Deewiant
Jul 07, 2007
ginophi
July 07, 2007
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?


July 07, 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.
July 07, 2007
Thanks for the quick reply. But I forgot to put a 'final' in there. Why would it work with 'const int b = 1;' and not 'final int b = 1;' ?

"Deewiant" <deewiant.doesnotlike.spam@gmail.com> wrote in message news:f6oqhj$rta$1@digitalmars.com...
> 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.


July 07, 2007
ginophi wrote:
> Thanks for the quick reply. But I forgot to put a 'final' in there. Why would it work with 'const int b = 1;' and not 'final int b = 1;' ?
> 

Because D isn't Java.

In D 1.0, which is what you should be using, final only has a meaning regarding classes and inheritance. See "Virtual Functions" at http://www.digitalmars.com/d/1.0/function.html and "Final Classes" at http://www.digitalmars.com/d/1.0/class.html

If you care about the beta 2.0, see http://www.digitalmars.com/d/final-const-invariant.html

-- 
Remove ".doesnotlike.spam" from the mail address.
July 07, 2007
Thanks I'll check that out


"Deewiant" <deewiant.doesnotlike.spam@gmail.com> wrote in message news:f6orl6$tm2$1@digitalmars.com...
> ginophi wrote:
> > Thanks for the quick reply. But I forgot to put a 'final' in there. Why would it work with 'const int b = 1;' and not 'final int b = 1;' ?
> >
>
> Because D isn't Java.
>
> In D 1.0, which is what you should be using, final only has a meaning
regarding
> classes and inheritance. See "Virtual Functions" at http://www.digitalmars.com/d/1.0/function.html and "Final Classes" at http://www.digitalmars.com/d/1.0/class.html
>
> If you care about the beta 2.0, see http://www.digitalmars.com/d/final-const-invariant.html
>
> -- 
> Remove ".doesnotlike.spam" from the mail address.