Hi! I've been having trouble all semester with knowing the scopes of different functions/variables and I have a particular question on one instance in my tic tac toe code. (Warning: I know it's not finished! I still have more to add, but I wanted to get this part correct before moving on)
I have a 2D vector named box, and I am trying to modify this vector throughout my code and have a bunch of functions that use the vector box. And all of these functions are changing it. How would I go about the function/variable prototypes in the beginning of my code in order to achieve this?
For the way I have it in the beginning of my code, I figured each function would be modifying the 2D box vector since the & operator has the address of the 2D vector. But it's most likely not correct since I'm getting the error in my prototype for box: 'box' declared as reference but not initialized'
I never had to initialize a 2D vector in the prototype before, so I don't understand the error.
box on line 16 is a reference. A reference has to always refer to an object and it's not possible to change which object it refers to later on, so for that reason you'll have to state what object it refers to right away when it's defined. You are creating a variable named box inside main that you pass to all the functions so it doesn't look like you actually need line 16 so you can probably just remove it.
My current issue with the tic tac toe board now is that when I play through the game, whenever a player wins, the game continues and print_win() doesn't seem to be called at all.
My compiler spits out a bunch of warnings in the win function.
warning: suggest parentheses around comparison in operand of ‘==’
The == operator takes two arguments and returns a bool (true or false).
a == b == c is equivalent to((a == b) == c).
(a == b) will return a bool so c will be compared to a bool.
If you want to compare if a, b and c are equal you'll have to split it into two comparisons and combine the result using the && (and) operator a == b && b == c.
warning: unused variable ‘winner’
char winner = box[0][0];
This creates a local variable that only exists within that if statement. If your intention was to assign the global winner variable just remove char from the beginning of the line.