0

I am trying to have a function change a 2D array that is part of another class, and I do not want to use a global variable.

My array contains actual values, but it is defined by

int cellGrid[101][101]; 

and the function I am trying to use looks like this

Cell::myCellFunction(int x, int y, int& cellGrid){
    cellGrid[x][y]--;
}

I attempted to pass a reference to the array as a parameter, but when I tried to access the data, I get this error

./cellFunctions.cpp:277:13: error: subscripted value is not an array, pointer, or vector
                        cellGrid[x][y]--;}
                        ~~~~~~~~^~

What is the best way to pass in a 2D array so that my function is able to change the actual array and not a copy?

Dale Larie
  • 33
  • 1
  • 7
  • 3
    Does this answer your question? [Passing a 2D array to a C++ function](https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function) (See second answer for actual pass by reference) – Lukas-T Jul 21 '20 at 20:14
  • yes it does, I didn't see the second answer when I came across this post earlier and the first one didn't seem like it would work for me. Thanks! – Dale Larie Jul 21 '20 at 20:21

0 Answers0