Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: Clyde on October 06, 2009

Title: [C++] error C2108: subscript is not of integral type.
Post by: Clyde on October 06, 2009
Hiya!

im having the error message error C2108: subscript is not of integral type with using the following code inside a loop.

Code: [Select]
int value1=34;
float XR[ 3 ];
float slut_c[360];
float x_cord[100];
float x1,y1;

for ( int a=0; a<100; a++)
{

        x1=x_cord[a]; //example 1
       
        y1=y1*slut_c[ XR[ value1 ] ];

}//next
Title: Re: [C++] error C2108: subscript is not of integral type.
Post by: hellfire on October 06, 2009
"XR[value1]" contains a float.
You need a cast to use it as an index:
Code: [Select]
y1=y1*slut_c[ (int)XR[ value1 ] ];
Title: Re: [C++] error C2108: subscript is not of integral type.
Post by: Clyde on October 07, 2009
Cheers dude!

So because an array no matter what kind of scope it has, as inside the brackets an array is an integer, it requires an (int) conversion?
Title: Re: [C++] error C2108: subscript is not of integral type.
Post by: Jim on October 07, 2009
Yeah, the index for an array (the bit inside the brackets) has to be an integer type (signed or unsigned) char, short, int, long, long long, not float, double or long double.

Jim
Title: Re: [C++] error C2108: subscript is not of integral type.
Post by: Rbz on October 07, 2009
It's for allowing things like this (float index for array) that give basic compilers bad reputation. I didn't believe freebasic would allow it but it did, without any warning :(