Pointer problem: if that's all you want to know, skip next paragraph and go straight to code.
As part of a tic-tac-toe system I am writing, I have a "board" object. It holds a 2d array of "cell" objects. The problem member function of "board" is "move()", which modifies to board. I also have an argument that is a pointer to a board. If this is not NULL, I want the state of the would-be changed board copied into the board the pointer points to. I'm having trouble with "if(not NULL)" part of the "move()" function. So, I have included the function and "board" 's overloaded operator =.
Oh, and I might want to add that "access()" is a private function that returns a reference to the corresponding cell in the board.
Code:
RETURN_NOTICE board::move(short int which_player, int x_in, int y_in, board* out = NULL)
{
board * temp;
if(x_in > 2 || y_in > 2 || x_in < 0 || y_in < 0 || which_player < 1 || which_player > 2) return OUT_OF_BOUNDS;
if(get_cell(x_in,y_in).is_occupied()) return NO_VACANCY;
if(out == NULL)
{
access(x_in, y_in)->assign((which_player == 1) ? P1_symbol : P2_symbol);
}
else
{
*temp = *this;
temp->move(which_player, x_in, y_in, NULL);
*out = *temp; // DEBUG -- ILLEGAL OPERATION HERE
}
return SUCCESS;
}
Code:
board board::operator = (board other)
{
cout << "P1_symbol = other.get_P1();" << endl;//DEBUG
P1_symbol = other.get_P1();
cout << "P2_symbol = other.get_P2();" << endl;//DEBUG
P2_symbol = other.get_P2();
for(int a = 0; a < 3; a++)
{
for(int b = 0; b < 3; b++)
{
cout << "*access(a,b) = other.get_cell(a,b);" << endl;//DEBUG
*access(a,b) = other.get_cell(a,b);
}
}
cout << "return *this;" << endl; //DEBUG
return *this;
}
and... for good measure... the object itself.
Code:
class board
{
cell cells[3][3];
char P1_symbol;
char P2_symbol;
inline cell * access(int x_in, int y_in);//for class to read/write itself in reg (x,y) plane style
public:
board();
board(char P1_in, char P2_in);
~board();
cell get_cell(int x_in, int y_in);
inline char get_P1() {return P1_symbol;}
inline char get_P2() {return P2_symbol;}
board operator = (board other);
RETURN_NOTICE move(short int which_player, int x_in, int y_in, board* out);
};