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. My Copy Constructor bug problem

My Copy Constructor bug problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebuggingc++cssgame-dev
9 Posts 5 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 currently writing a program tictactoe for windows. i got mostly everything working. i got the program to draw the board and draw the X's and O's. the game start by beginning with the player then wait for the computer to move. i got the part where the player clicks on the board and an X is drawn in the grid. i only have to make the computer move but Everything seems to work OK, until last nite when I created a function CheckForMoves which returns an integer and also returns the row and column which is best move. inside the function CheckForMoves, i created a Board then i assigned it to a pointer to Board like this: CBoard board = *pBoard; pBoard is the Board i am using to draw my X's and O's for the game. i am copying it to a new board so i can try out all the possible moves in a new board without touching the board i am using in the game. because my board class was implemented dynamically, i wrote my own assignment operator and my own copy constructor. in my assignment operator function, i delete what ever the pointer is point to, then call new then point it to whatever is in the right of the operator. Copy constructor, because it is empty, i just point the pointers to what ever is on the right side. i went along and debugged it using the VC++ debugger then when i was walking through the code i saw that it was calling the Copy constructor. then as i walk over the Copy Constructor everything seems fine. by using asserts, if found out that all the pointers are being copied right. the cells where i store the X's and O's are copied right too. i tried using TRACE statements but i cant find the bug causing the BOARD TO BE EMPTY. i tried using MessageBox before and after calling the copy constructor. as i moved the MessageBox i saw my move X on the Board. but as soon as i clicked the MessageBox that Outputs "After" called after the copy constructor was called. the X in the board disappears. MY CONCLUSION IS THE BOARD I AM USING IN THE GAME IS BEING CHANGED SOMEHOW IN THE COPY CONSTRUCTOR. the copy constructors parameters is like this: CBoard& CBoard::CBoard(const CBoard& Copy) I Hope this helps explain my problem. sorry if its very long, i just want to explain it with much detail so i can tell you whats going on. Can anyone tell me whats wrong wid my program and how i can fix it? i would appreciate any method on how to find the bug. Thank you very very much. John

    P 1 Reply Last reply
    0
    • J John Cruz

      hello. i am currently writing a program tictactoe for windows. i got mostly everything working. i got the program to draw the board and draw the X's and O's. the game start by beginning with the player then wait for the computer to move. i got the part where the player clicks on the board and an X is drawn in the grid. i only have to make the computer move but Everything seems to work OK, until last nite when I created a function CheckForMoves which returns an integer and also returns the row and column which is best move. inside the function CheckForMoves, i created a Board then i assigned it to a pointer to Board like this: CBoard board = *pBoard; pBoard is the Board i am using to draw my X's and O's for the game. i am copying it to a new board so i can try out all the possible moves in a new board without touching the board i am using in the game. because my board class was implemented dynamically, i wrote my own assignment operator and my own copy constructor. in my assignment operator function, i delete what ever the pointer is point to, then call new then point it to whatever is in the right of the operator. Copy constructor, because it is empty, i just point the pointers to what ever is on the right side. i went along and debugged it using the VC++ debugger then when i was walking through the code i saw that it was calling the Copy constructor. then as i walk over the Copy Constructor everything seems fine. by using asserts, if found out that all the pointers are being copied right. the cells where i store the X's and O's are copied right too. i tried using TRACE statements but i cant find the bug causing the BOARD TO BE EMPTY. i tried using MessageBox before and after calling the copy constructor. as i moved the MessageBox i saw my move X on the Board. but as soon as i clicked the MessageBox that Outputs "After" called after the copy constructor was called. the X in the board disappears. MY CONCLUSION IS THE BOARD I AM USING IN THE GAME IS BEING CHANGED SOMEHOW IN THE COPY CONSTRUCTOR. the copy constructors parameters is like this: CBoard& CBoard::CBoard(const CBoard& Copy) I Hope this helps explain my problem. sorry if its very long, i just want to explain it with much detail so i can tell you whats going on. Can anyone tell me whats wrong wid my program and how i can fix it? i would appreciate any method on how to find the bug. Thank you very very much. John

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #2

      John Cruz wrote: CBoard& CBoard::CBoard(const CBoard& Copy) I am not sure if this is causing you problems, but this is not what a copy constructor looks like a copy constructor has no return value. CBoard::CBoard(const CBoard& Copy) { ... } Maybe the compiler is generating the copy constructor for you and that is where your problem is.

      J 1 Reply Last reply
      0
      • P Paul M Watt

        John Cruz wrote: CBoard& CBoard::CBoard(const CBoard& Copy) I am not sure if this is causing you problems, but this is not what a copy constructor looks like a copy constructor has no return value. CBoard::CBoard(const CBoard& Copy) { ... } Maybe the compiler is generating the copy constructor for you and that is where your problem is.

        J Offline
        J Offline
        John Cruz
        wrote on last edited by
        #3

        Oh, i am very sorry... i made a mistake in typing my copy constructor... my copy constructor does look like the one u have. here it is and this is whats inside. CBoard::CBoard(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; // Set the values to default for now mpDefaultPen = NULL; mpDefaultFont = NULL; // 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]; } } thank you very much.

        A 1 Reply Last reply
        0
        • J John Cruz

          Oh, i am very sorry... i made a mistake in typing my copy constructor... my copy constructor does look like the one u have. here it is and this is whats inside. CBoard::CBoard(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; // Set the values to default for now mpDefaultPen = NULL; mpDefaultFont = NULL; // 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]; } } thank you very much.

          A Offline
          A Offline
          Alexandru Savescu
          wrote on last edited by
          #4

          Hello John, I am not sure what your real problem is and if what I am telling will give you an answer to your solution. But from your code I draw the conclusion that mpPen are created on the heap. When you do: John Cruz wrote: // Create new objects mpPen = new CPen; mpFont = new CFont; you create two pointers on the heap. Then on John Cruz wrote: // Set the values to the new values mpPen = Copy.mpPen; mpFont = Copy.mpFont; you equal the pointers not their values. Futhermore you allocate memory (new CPen) and you do not use it because you assign the pointer to the memory referenced my the source CBoard object (myPen = Copy.mpPen). When the source copy goes out of scope the destination Pen and destination Font will point to rubbish. Try *mpPen = *Copy.mpPen; But since these are GDI object you must take into account the handles (HPEN, HFONT), I am not sure, but I think you just cannot copy these objects as the predefined types. I hope this helps, Best regards, Alexandru Savescu

          J 1 Reply Last reply
          0
          • A Alexandru Savescu

            Hello John, I am not sure what your real problem is and if what I am telling will give you an answer to your solution. But from your code I draw the conclusion that mpPen are created on the heap. When you do: John Cruz wrote: // Create new objects mpPen = new CPen; mpFont = new CFont; you create two pointers on the heap. Then on John Cruz wrote: // Set the values to the new values mpPen = Copy.mpPen; mpFont = Copy.mpFont; you equal the pointers not their values. Futhermore you allocate memory (new CPen) and you do not use it because you assign the pointer to the memory referenced my the source CBoard object (myPen = Copy.mpPen). When the source copy goes out of scope the destination Pen and destination Font will point to rubbish. Try *mpPen = *Copy.mpPen; But since these are GDI object you must take into account the handles (HPEN, HFONT), I am not sure, but I think you just cannot copy these objects as the predefined types. I hope this helps, Best regards, Alexandru Savescu

            J Offline
            J Offline
            John Cruz
            wrote on last edited by
            #5

            ok ill try that alexandru. :-) Thank you very much, John

            J 1 Reply Last reply
            0
            • J John Cruz

              ok ill try that alexandru. :-) Thank you very much, John

              J Offline
              J Offline
              John Cruz
              wrote on last edited by
              #6

              I have tried it but the compiler gave me an error saying operator= not available. Does CPen and CFont have an Assignment operator... here is what i did.. // Create new objects mpPen = new CPen; mpFont = new CFont; // Set the values to the new values *mpPen = *Copy.mpPen; *mpFont = *Copy.mpFont; Thank you very much, John

              A 1 Reply Last reply
              0
              • J John Cruz

                I have tried it but the compiler gave me an error saying operator= not available. Does CPen and CFont have an Assignment operator... here is what i did.. // Create new objects mpPen = new CPen; mpFont = new CFont; // Set the values to the new values *mpPen = *Copy.mpPen; *mpFont = *Copy.mpFont; Thank you very much, John

                A Offline
                A Offline
                Andrew Peace
                wrote on last edited by
                #7

                Why are you storing pen/font objects as part of the class anyway - surely they should be variables local to your drawing function? You could always have CreatePen() and CreateFont() member functions, but better not to use pointers for pen/font objects at all - there's no need, as long as you properly deselect them from the device contexts. -- Andrew.

                J 1 Reply Last reply
                0
                • A Andrew Peace

                  Why are you storing pen/font objects as part of the class anyway - surely they should be variables local to your drawing function? You could always have CreatePen() and CreateFont() member functions, but better not to use pointers for pen/font objects at all - there's no need, as long as you properly deselect them from the device contexts. -- Andrew.

                  J Offline
                  J Offline
                  John Cruz
                  wrote on last edited by
                  #8

                  I think now i see that i have a bad program design. my whole tictactoe design is wrong because i only made 3 objects. Board, Player, and Computer. Board handles all the drawing on the board, Player handles all the player moves and stuff. Computer is derived from the Player but it has some additional functions like the Alpha-beta pruning function. IS THERE ANYWAY I CAN FIX MY PROBLEM WITHOUT TRASHING ALL MY CODE. i am sorry, i am still new at this stuff... Thank you very much, John

                  T 1 Reply Last reply
                  0
                  • J John Cruz

                    I think now i see that i have a bad program design. my whole tictactoe design is wrong because i only made 3 objects. Board, Player, and Computer. Board handles all the drawing on the board, Player handles all the player moves and stuff. Computer is derived from the Player but it has some additional functions like the Alpha-beta pruning function. IS THERE ANYWAY I CAN FIX MY PROBLEM WITHOUT TRASHING ALL MY CODE. i am sorry, i am still new at this stuff... Thank you very much, John

                    T Offline
                    T Offline
                    Tim Smith
                    wrote on last edited by
                    #9

                    Heh, please don't take this as if I am being flippant. Even after nearly 20 years of programming, I still find that many of my initial designs are junk and have to be thrown away. Sometimes in the long run you save more time by starting again with good solid design than trying to patch together a bad design. Then again, if you actually have to maintain the code, it is always best to work from a good solid design. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

                    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