Help finding a Copy Constructor bug...
-
hello. i am trying to write an alpha-beta function called CheckForMove that searches the best move on the board. here is my function... // Searches for the best computer move on the board int CComputer::CheckForMoves(int Depth, int Alpha, int Beta, int& Row, int& Col) { // Check if the Entered Depth is greater than the Maximum depth ASSERT(Depth >= MAXDEPTH); // point the board to a new board CBoard Board = *mpBoard; <-- here is the problem // Test for the values for now Alpha = Beta = 0; Row = Col = 1; return 0; } when i created a new board and pointed mpBoard which is a Global member function to the Computer class, something is going wrong. so i tried commenting out that and the whole program works. its seems like the problem was on the Copy constructor, because its not copying it correctly i decided to write a new one. it wrote a copy constructor but its still not working write. // Copy Constructor CBoard::CBoard(const CBoard& Original) { // Copy all the values Copy(Original); } // Copys all the objects void CBoard::Clone(const CBoard& Copy) { // Create new objects mpPen = new CPen; mpFont = new CFont; // Set the values to the new values mpPen = Copy.mpPen; mpFont = Copy.mpFont; // Sets the rectangle to a certain point, width and height for (int r = 0; r < MAXROW; r++) for (int c = 0; c < MAXCOL; c++) { mCell[r][c] = Copy.mCell[r][c]; mpGrid[r][c] = new CRect( mLeftTopPt,CSize (mCellWidth,mCellHt)); mpGrid[r][c] = Copy.mpGrid[r][c]; } } This function compiles correctly. When i use to function CheckforMove it returns the row and column but, its not Drawing the board. i think somehow the mpBoard is getting overwritten because nothing the board is being drawn right. am i missing anything in my Copy Constructor or something. i tried tracing it and putting assert statements but i cant find the bug. can anyone Help me?
-
hello. i am trying to write an alpha-beta function called CheckForMove that searches the best move on the board. here is my function... // Searches for the best computer move on the board int CComputer::CheckForMoves(int Depth, int Alpha, int Beta, int& Row, int& Col) { // Check if the Entered Depth is greater than the Maximum depth ASSERT(Depth >= MAXDEPTH); // point the board to a new board CBoard Board = *mpBoard; <-- here is the problem // Test for the values for now Alpha = Beta = 0; Row = Col = 1; return 0; } when i created a new board and pointed mpBoard which is a Global member function to the Computer class, something is going wrong. so i tried commenting out that and the whole program works. its seems like the problem was on the Copy constructor, because its not copying it correctly i decided to write a new one. it wrote a copy constructor but its still not working write. // Copy Constructor CBoard::CBoard(const CBoard& Original) { // Copy all the values Copy(Original); } // Copys all the objects void CBoard::Clone(const CBoard& Copy) { // Create new objects mpPen = new CPen; mpFont = new CFont; // Set the values to the new values mpPen = Copy.mpPen; mpFont = Copy.mpFont; // Sets the rectangle to a certain point, width and height for (int r = 0; r < MAXROW; r++) for (int c = 0; c < MAXCOL; c++) { mCell[r][c] = Copy.mCell[r][c]; mpGrid[r][c] = new CRect( mLeftTopPt,CSize (mCellWidth,mCellHt)); mpGrid[r][c] = Copy.mpGrid[r][c]; } } This function compiles correctly. When i use to function CheckforMove it returns the row and column but, its not Drawing the board. i think somehow the mpBoard is getting overwritten because nothing the board is being drawn right. am i missing anything in my Copy Constructor or something. i tried tracing it and putting assert statements but i cant find the bug. can anyone Help me?
John Cruz wrote: // point the board to a new board CBoard Board = *mpBoard; <-- here is the problem You have to define an assignment operator in your CBoard class as the above code calls it
CBoard& CBoard::operator=(CBoard&)
If there is no overloaded operator= member function then there is a bitwise member copy which is dangerous if you have pointer data members. Atul Dharne