Having difficulty with constructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackstormdragon
    New Member
    • Feb 2007
    • 32

    Having difficulty with constructor

    Okay I have to make this TicTacToe program. Everything works except my constructor(whi ch must initialize my empty board to all zeros) This is where I run into problems. For some reason my constructor won't do anything. Everything looks right, so I don't know what to do. Here's a snipit of my code.
    Code:
    class TicTacToe
    {
    public:
    	TicTacToe();
    	int checkWinner();
    	void printBoard();
    	void playGame();
    private:
    	char board[3][3];
    	bool winner;
    	int row, column, turn;  
    };
    
    int main()
    {
    	TicTacToe game;
    
    	game.playGame(); 
    
    }
    TicTacToe::TicTacToe()
    {
    
    	 for(int index = 0; index < 3; index++)
         {
             for(int index2 = 0; index2 < 3; index2++)
             {
                 board[index][index2] = 0;
             }
    	 }
    }
    Thanks
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by blackstormdrago n
    Okay I have to make this TicTacToe program. Everything works except my constructor(whi ch must initialize my empty board to all zeros) This is where I run into problems. For some reason my constructor won't do anything. Everything looks right, so I don't know what to do. Here's a snipit of my code.
    Code:
    class TicTacToe
    {
    public:
    	TicTacToe();
    	int checkWinner();
    	void printBoard();
    	void playGame();
    private:
    	char board[3][3];
    	bool winner;
    	int row, column, turn;  
    };
    
    int main()
    {
    	TicTacToe game;
    
    	game.playGame(); 
    
    }
    TicTacToe::TicTacToe()
    {
    
    	 for(int index = 0; index < 3; index++)
         {
             for(int index2 = 0; index2 < 3; index2++)
             {
                 board[index][index2] = 0;
             }
    	 }
    }
    Thanks
    Since yout board variable is a char [][] try initializing it with '0':
    [code=cpp]
    board[index][index2] = '0';
    [/code]
    Other than that, it looks fine.

    Comment

    • blackstormdragon
      New Member
      • Feb 2007
      • 32

      #3
      Originally posted by ilikepython
      Since yout board variable is a char [][] try initializing it with '0':
      [code=cpp]
      board[index][index2] = '0';
      [/code]
      Other than that, it looks fine.
      Yeah that was it. Thanks. I can't belive I missed it.

      Comment

      Working...