Like Tree3Likes
  • 1 Post By rogster001
  • 1 Post By laserlight
  • 1 Post By rogster001

How do you declare an array in a class?

This is a discussion on How do you declare an array in a class? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm creating a Tic Tac Toe game and the problem I have so far is that I cant find ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    How do you declare an array in a class?

    Hi, I'm creating a Tic Tac Toe game and the problem I have so far is that I cant find out how to declare my array for the board in my class =( Here is my code so far. I am getting an error saying that 'Board isnt declared in this scope' so could somebody please help me with declaring Board within my class?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class PlayerClass{
        public: //public consists of functions within the playerclass
    
    
        //initilises the board at the start of the game to be all 0
       void initialBoard(){
        int Board[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
        for(int row = 0; row < 3; row++){
            cout<<"                    ";
        for(int column = 0; column < 3; column++){
        cout<<Board[row][column]<< " ";}
        cout<<endl;}}
    
    
        //prints out the board to show changed when a player move has made
        void printBoard(int Board[3][3]){
        for(int row = 0; row < 3; row++){
            cout<<"                    ";
        for(int column = 0; column<3;column++){
        cout<<Board[row][column]<< " ";}
        cout<<endl;}}
    
    
    
        //function to ask the player where they wish to place their shape and sets it to the board
        void PlayerTurn(int turn){
            if (turn == 1){cout<<"which position do you want to place your letter"<<endl;
            cout<<"enter in x coordinate";
            cin>>x;
            if(x<1||x>3){cout<<"please enter number between 1 and 3";
            cin>>x;}
            cout<<"enter in y coordinate";
            cin>>y;
            if (y<1||y>3){cout<<"please enter in a valid coordinate between 1 and 3";
            cin>>y;}
            else
            Board[x][y]=player1;
            }
    
            else
            if (turn == 2){cout<<"which position do you want to place your letter"<<endl;
            cout<<"enter in x coordinate";
            cin>>x;
            if(x<1||x>3){cout<<"please enter number between 1 and 3";
            cin>>x;}
            cout<<"enter in y coordinate";
            cin>>y;
            if (y<1||y>3){cout<<"please enter in a valid coordinate between 1 and 3";
            cin>>y;}
            else
            Board[x][y]=player2;}}
    
    
    
          private: //private consists of variables used by the functions playerclass
            char move;
            int x;
            int y;
            int turn;
            char player1;
            char player2;
                    };
    
    
    
    
    
    int main()
    {
    int Board[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
    
    PlayerClass Pmove;
    cout<<"                 Tic-Tac-Toe"<<endl<<endl;
    
    Pmove.initialBoard();
    
    Pmove.PlayerTurn(1);
    Pmove.printBoard(Board); //needs to print the board to show where player has moved
    Pmove.PlayerTurn(2);
        return 0;
    }
    Thanks so much =)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,713
    Quote Originally Posted by casablanca
    How do you declare an array in a class?
    Similiar to how you would declare an array outside of a class, except that it would be declared as a member (like your other member variables) and you cannot initialise the array in the declaration.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    I am getting an error saying that 'Board isnt declared in this scope'
    In your class definition you declare board local to the constructor, you also declare an array named 'board' in your main (), this is a way to do it.

    Code:
    class MyGame
    {
      public:
      int board[3][3];
      
      void InitBoard();
      
      MyGame();
    };
    
    MyGame::MyGame()
    {
        //stuff...
        //..stuff...
        InitBoard();
    }
    
    void MyGame::InitBoard()
    {
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                board[i][j] = 0;
            }
        }
    }
    
    int main()
    {
        MyGame game;
        
        game.board[0][0] = 1; //for example access...
    }
    Last edited by rogster001; 08-02-2011 at 05:38 AM.
    Salem likes this.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,713
    In this special case of initialising to 0, you can use the initialisation list for value initialisation instead of calling InitBoard, e.g.,
    Code:
    MyGame::MyGame() : board()
    {
        //stuff...
        //..stuff...
    }
    iMalc likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    The indentation is horrible and unreadable. You should learn from rogster001, whose indentation is very clean and nice.
    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.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    Sorry lol I use the compiler Codeblocks and if my indentation is horrible blame it on the compiler and not me! I cant be bothered to press tab every time I create a new line..

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    Thanks for eveybodys help, I will hopfuly be able to get this array thing working =D

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Sorry lol I use the compiler Codeblocks and if my indentation is horrible blame it on the compiler and not me! I cant be bothered to press tab every time I create a new line..
    I use code blocks too...the indentation is nothing to do with the 'compiler'..! Am sure you meant 'editor' but still thats not to blame..unless you have chosen a setting 'de-indent' my code!
    stahta01 likes this.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,569
    It is a poor workman who blames his tools for his problems.

    Tim S.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Quote Originally Posted by casablanca View Post
    I cant be bothered to press tab every time I create a new line..
    Then you can't be bothered to become a programmer.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to declare constant strings in a class?
    By bling in forum C++ Programming
    Replies: 3
    Last Post: 08-11-2008, 05:56 PM
  2. Replies: 5
    Last Post: 04-17-2008, 02:31 AM
  3. accessing/declare protected class in class
    By Hork83 in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2007, 10:42 AM
  4. Derived class can't declare
    By hdragon in forum Game Programming
    Replies: 13
    Last Post: 03-21-2006, 01:48 PM
  5. how to declare this class function
    By axon in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 11:15 AM

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