Thread: Trouble initialiazing an array with constants

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    Question Trouble initialiazing an array with constants

    This is my first time posting, I hope I got it right
    This is my header:
    Code:
     
    #ifndef TTT_H
    #define TTT_H
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    class TicTacToe
    {
    public:
    
    static const size_t rows = 3;
    static const size_t cols = 3;
    //These are static constants to represent the number of rows and columns in our Tic-Tac-Toe board. 
    
    
    TicTacToe ();
    // This is the default constructor for the class. 
    // It will call a function to initialize the Tic-Tac-Toe board to blank spaces (i.e. the ' ' character).
    
    void InitializeBoard();
    //This function will initialize the board to blank spaces.
    //It is also called by the default constructor above.
    
    
    
    void PrintBoard();
    // This function will print the Tic-Tac-Toe board.
    
    bool OccupySquare(size_t x, size_t y, char value);
    //This function will occupy a square on the board. 
    //It takes in two coordinates and a value which should be either 'X' or 'O'.
    //It returns true if the square was occupied, false if the square wasn't occupied
    
    char CheckForWinner();
    //This function will check the board for a winner. 
    //It will return a ' ' to indicate no winner, an 'X' to indicate that X has won,
    //an 'O' to indicate that O has won, or an 'S' to indicate a scratch.
    
    private:
    char board[rows][cols];
    //This is a two-dimensional character array to represent the board. 
    
    
    bool IsValidLocation(size_t x, size_t y);
    //This function will take in two coordinates and decide if they are valid. 
    //Valid coordinates are those within the range of the board indices and not already occupied. 
    //This function will be called by OccupySquare(). It will return true if the coordinates are valid, false otherwise.
    
    bool IsScratch();
    //This function will decide if the game is a scratch. 
    //It will be called by CheckForWinner().
    //Note that you should call this function after checking the winning possibilities.
    
    };
    #endif
    these are my implementation definitions
    Code:
     
    #include <iostream>
    using namespace std;
    #include "ttt.h"
    
    
    TicTacToe::TicTacToe ()
    {
    InitializeBoard ();
    }
    void TicTacToe::InitializeBoard()
    {
    	for (rows = 0; rows < 3; rows ++)//outer loop for row
    	{
    	 for (cols = 0; cols <3; cols ++) //inner loop for column
    	 { 
                     board [rows][cols] = NULL ;}  //board intialized 
    	  }
    }
    
    void TicTacToe::PrintBoard()
    {
    	for ( rows = 0; rows < 3; rows ++)//outer loop for row
    	{cout << "      ", board[rows][0], board [rows][1], board[rows][2];
    	 if (rows != 2)
    	 {
    		 cout << "\n ___I___I___\n" <<endl;
    	 }
    	}
    }
    }

    My Problem is that the constants rows and variables wont be assigned values

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No idea what you mean. But fix your indentation. You'll see that there is a mismatched brace. That is why you ALWAYS indent properly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    Fixed spacing and a more specific post of the problem

    Sorry, I fixed my spacing and and brackets, and here is the conflict:
    This is from my file containing a function that initializes my array
    Code:
    void TicTacToe::InitializeBoard()
        {
         for (rows = 0; rows < 3; rows ++)
    	   {
    	    for (cols = 0; cols <3; cols ++){ board [rows][cols] = NULL ;}  	    
    	   }
         }
    And this is from my class in a seperate header
    Code:
    
    class TicTacToe
    {
    public:
    
    static const size_t rows = 3;
    static const size_t cols = 3;
    //These are static constants to represent the number of rows and columns in our Tic-Tac-Toe board. 
    
    
    private:
    char board[rows][cols];
    My error message regarding this is :
    c:\users\jennifer\documents\visual studio 2008\projects\tictactoe\tictactoe\ttt.cpp(13) : error C3892: 'rows' : you cannot assign to a variable that is const
    c:\users\jennifer\documents\visual studio 2008\projects\tictactoe\tictactoe\ttt.cpp(13) : error C2105: '++' needs l-value
    c:\users\jennifer\documents\visual studio 2008\projects\tictactoe\tictactoe\ttt.cpp(15) : error C3892: 'cols' : you cannot assign to a variable that is const
    c:\users\jennifer\documents\visual studio 2008\projects\tictactoe\tictactoe\ttt.cpp(15) : error C2105: '++' needs l-value

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    final

    Did you get the answer to this? if so what is it?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    For in class constants, I recommend you use the enum approach Stroustrup discusses here. Incidentally since rows and cols are the same, and I've never seen a tic-tac-toe game on anything besides a square, the definition is:
    Code:
    enum {
        SIDE = 3
    };
    char board[SIDE][SIDE];
    Although, you may want to support larger squares ....

    The other errors are about your use of the rows and cols constants.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    Hi there,

    As suggested above the errors are basically related to constants. As you know you can't change the value of a constant once you assigned a value to it. So it''s no use to try to change its value on your code.(i.e. you can't do rows = 0,cols = 0, rows++ and cols++)

    If you don't like using enumerations as suggested above, you may want to stop using constants like variables. Rather you may want to declare new variables like int rows_ = 0; rather than rows = 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. trouble creating an array
    By s_ny33 in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2005, 11:18 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

Tags for this Thread