Constructor problem

This is a discussion on Constructor problem within the C++ Programming forums, part of the General Programming Boards category; In that case, step through with a debugger....

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In that case, step through with a debugger.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    "An access violation (Segmentation fault) raised in your program"

    Mean anything to anyone?

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code:
    TicTacToe:: TicTacToe ()
    {        
      int numOfRows;
      int numOfCols;
      // Initialising TicTacToe //    
      for (int r = 0; r < numOfRows; r++) //looping rows
        for (int c = 0; c < numOfCols; c++) //looping columns
          board[r][c] = ' ';
    }
    Since you don't initialize numOfRows or numOfCols, they have random values. Thus, you're accessing memory that doesn't belong to you in the loop.

    Code:
    const int NUM_ROWS = 3;
    const int NUM_COLS = 3;
    ...
         char board [NUM_ROWS] [NUM_COLS];
    ...
    TicTacToe:: TicTacToe ()
    {        
      // Initialising TicTacToe //    
      for (int r = 0; r < NUM_ROWS; r++) //looping rows
        for (int c = 0; c < NUM_COLS; c++) //looping columns
          board[r][c] = ' ';
    }
    Replace all other usages of the number 3 as appropriate.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #19
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Sorry to be a pain!

    But another error - just when you think you are sorted:

    - ISO C++ forbids initialization of member `NUM_ROWS'

    Here
    const int NUM_ROWS = 3;

    and

    ISO C++ forbids initialization of member `NUM_COLS'

    const int NUM_COLS = 3;

    Code:
    using namespace std;
    #include <iomanip>
    class TicTacToe
    {
       private:
         enum Status {WIN, DRAW, CONTINUE };
         const int NUM_ROWS = 3;
         const int NUM_COLS = 3;
    
       public:
         TicTacToe();
         void makeMove ();
         void printBoard ();
         bool validMove (int r, int c);
         bool xoMove (int input);
         Status gameStatus ();
         void holdscreen ();
         char board [NUM_ROWS] [NUM_COLS];
    };
     
    TicTacToe:: TicTacToe ()
    {        
      // Initialising TicTacToe //    
      for (int r = 0; r < NUM_ROWS; r++) //looping rows
        for (int c = 0; c < NUM_COLS; c++) //looping columns
          board[r][c] = ' ';
    }
    Any ideas on these errors? Tried googling for these errors, but to no avail?

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Put a static before them.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    That's got it - cheers for your help. Before tried deleting const and adding static, not putting static before const

    Thanks again

  7. #22
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by rebel
    Sorry to be a pain!

    But another error - just when you think you are sorted:

    - ISO C++ forbids initialization of member `NUM_ROWS'

    Here
    const int NUM_ROWS = 3;

    and

    ISO C++ forbids initialization of member `NUM_COLS'

    const int NUM_COLS = 3;

    Code:
    using namespace std;
    #include <iomanip>
    class TicTacToe
    {
       private:
         enum Status {WIN, DRAW, CONTINUE };
         const int NUM_ROWS = 3;
         const int NUM_COLS = 3;
    
       public:
         TicTacToe();
         void makeMove ();
         void printBoard ();
         bool validMove (int r, int c);
         bool xoMove (int input);
         Status gameStatus ();
         void holdscreen ();
         char board [NUM_ROWS] [NUM_COLS];
    };
     
    TicTacToe:: TicTacToe ()
    {        
      // Initialising TicTacToe //    
      for (int r = 0; r < NUM_ROWS; r++) //looping rows
        for (int c = 0; c < NUM_COLS; c++) //looping columns
          board[r][c] = ' ';
    }
    Any ideas on these errors? Tried googling for these errors, but to no avail?
    If you want to declare those members as const... You can't initialize data members in that manner, they need to be initialized in the class' constructor. And for const data members, you need to use an initialization list.

    Code:
    class TicTacToe
    {
       private:
         enum Status {WIN, DRAW, CONTINUE };
         const int NUM_ROWS;
         const int NUM_COLS;
    
       public:
         TicTacToe();
         void makeMove ();
         void printBoard ();
         bool validMove (int r, int c);
         bool xoMove (int input);
         Status gameStatus ();
         void holdscreen ();
         char board [NUM_ROWS] [NUM_COLS];
    };
    
    TicTacToe:: TicTacToe () : NUM_COLS(3), NUM_ROWS(3)
    {        
      // Initialising TicTacToe //    
      for (int r = 0; r < NUM_ROWS; r++) //looping rows
        for (int c = 0; c < NUM_COLS; c++) //looping columns
          board[r][c] = ' ';
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Considering that his code is bound to a board size of 3 (gameStatus assumes this), it doesn't make sense to store the board size constants per object.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21