views:

188

answers:

4

Greetings everyone. Having an issue compiling my script containing the following function. Three errors occur, all on the same line where I set distance += to distances [][]:

error C2108: subscript is not of integral type error C2108: subscript is not of integral type error C2297: '+=' : illegal, right operand has type 'double (*)[15]'

Assistance would be much appriciated.

double S_initial;

double distances [15][15]; 
double order [15];
void Initialize() 
{
    double x, y ,z;

    double distance = 0;

    for (int i = 0; i <= 14; i++)
    {
     x =  order [i];
     y =  order [i + 1];

     distance += distances [x][y];
    }

    S_initial = distance;
}
+1  A: 

Stop using double and use int instead.

Or if you have to use double in the order array, you need to decide how to round any non-integer value that may be found in order to a int. Math.Floor, Math.Ceiling etc.

AnthonyWJones
+2  A: 

x and y are not integers... You need to pass integers as array subscripts.

Aamir
+2  A: 

Well, the array subscripts x and y are not of an integral type like int, but of type double:

double x, y, z;
...
distance += distances[x][y];

And something like the 1.46534th element of an array doesn't make sense, so the compiler complains.

sth
A: 

You cannot use floating point numbers to index into arrays. Use int or even better size_t.

 for (int i = 0; i <= 14; i++)
{
    x =  order [i];
    y =  order [i + 1]; /* when i = 14, you invoke UB */

    distance += distances [x][y];
}

On to the second part:

double order [15];

is uninitialized and hence invokes UB, when used.

dirkgently