Cam9191
Cam9191

Reputation: 33

Invalid types `int[int]' for array subscript and others

The errors I keep getting are:

invalid types int[int]' for array subscript invalid conversion fromint' to int*' initializing argument 1 ofvoid slideLeft

I declared the variable, boardSize equals 0 in the int main() before the while loop.

Here are the some codes for you guys to check out:

void slideLeft(int board[], int i, int rowBeginIndex)
{
    while((board[i - 1] == 0) && (i > rowBeginIndex)) 
    {
        board[i - 1] = board[i];
        board[i] = 0;
        i--;
    }
}

in the int main():

while(GetInput(direction) != 0)
{
    cout << "Your move: \n\n";
    cin >> direction;

    if(direction == 'a')
    {
         for(row=0; row<RowSize; row++)
         {
              for(col=0; col<RowSize; col++)
              {
                   current = row*RowSize + col;
                   if(boardSize[current] != 0) 
                   {
                       slideLeft(boardSize, current, row*RowSize);
                   }
              }
         }
    }

Upvotes: 0

Views: 203

Answers (2)

tsragravorogh
tsragravorogh

Reputation: 3153

void slideLeft(int board[], int i, int rowBeginIndex) - this type of declaration will not work in C++. If you have an int array, the best way to take it as a function argument is if you take it as int*. Also, you need to pass the size of the array so that your function will not override data that does not belong to it. In short, here is what your function should look like:

void slideLeft(int *board, int boardSize, int i, int rowBeginIndex);

A question might arise how are you going to pass your array to this function. Here is how you do it:

int array[10];
slideLeft(&array, sizeof(array)/sizeof(int), i, rowBeginIndex);

If you declare an array the way I am doing above, then to get the number of elements in array you divide the complete size of the array to the size of the element that the array is of type (int).

Hope this helps.

Upvotes: 0

wavemode
wavemode

Reputation: 2116

The type mismatch comes from the fact that boardSize is simply an integer (set to 0 as you said), but your slideLeft function is trying to process an array. Pass the board itself in, not the size.

Upvotes: 2

Related Questions