I'm new to C++, and I have a question that should be easy, but it's making me crazy. I'm trying to set up a 2D array. The array is declared in Solver.h
like so:
protected:
static const int gridSize = 9;
int theGrid[gridSize][gridSize]
int *boxes[gridSize][gridSize];
...
and I'm trying to initialize it in Solver::Solver()
like so:
boxes[0] ={ &theGrid[0][0],&theGrid[0][1],&theGrid[0][2],
&theGrid[1][0],&theGrid[1][1],&theGrid[1][2],
&theGrid[2][0],&theGrid[2][1],&theGrid[2][2]
};
...
But the error I'm getting says "expression must be a modifiable lvalue" and "error: expected an expression". This didn't make sense to me, because I was under the impression that boxes[0]
was a modifiable lvalue.
So I wrote a tiny little (non OOP) program that just has the following in it's main()
:
int test[2][2];
test[0]= {1,2};
cout<<test[0][1]; //outputs "2" as expected.
And now I'm stuck and confused. What is wrong with my assignment routine in the Solver class?
To answer some questions:
I do want a multidimensional array, because eventually I'll be setting up boxes[1]
with another array of pointers to another set of data inside the theGrid
, a third set in boxes[2]
and so on. The idea is to take a 9x9 grid and split it into 3x3 squares (sound familiar? "Solver" should solve sudoku sets simply.) so I can check (and modify) all the values in those 3x3 squares independently.
I'm trying to get this working in VS2010.
I'm sure there's a good algorithm to define which array members are part of each 3x3 "box" based on that boxes number but I haven't hit it yet, and it seems less wasteful to code it once than to have the program re-create those boxes in loops every time it checks an answer. For the scope of this program the size of theGrid
is inalterable, so my conscience will let me get away with a little hard coding.