Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help finding a Copy Constructor bug...

Help finding a Copy Constructor bug...

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++graphicsbeta-testingquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Cruz
    wrote on last edited by
    #1

    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?

    A 1 Reply Last reply
    0
    • J John Cruz

      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?

      A Offline
      A Offline
      Atul Dharne
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups