Author Topic: [C++] error C2108: subscript is not of integral type.  (Read 15225 times)

0 Members and 1 Guest are viewing this topic.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
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
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1292
  • Karma: 466
    • View Profile
    • my stuff
Re: [C++] error C2108: subscript is not of integral type.
« Reply #1 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 ] ];
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] error C2108: subscript is not of integral type.
« Reply #2 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?
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] error C2108: subscript is not of integral type.
« Reply #3 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
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2750
  • Karma: 493
    • View Profile
    • http://www.rbraz.com/
Re: [C++] error C2108: subscript is not of integral type.
« Reply #4 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 :(
Challenge Trophies Won: