DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Using 2D Arrays inside Classes (http://www.daniweb.com/forums/thread158873.html)

MylesDBaker Nov 21st, 2008 9:56 pm
Using 2D Arrays inside Classes
 
Hello, I am trying to declare a 2D array (3x3) inside of a class.

I have it set up like this:

GameBoard.cpp
#include "GameBoard.h"
#include <iostream>

using namespace std;

char board [3][3];

GameBoard::GameBoard()
{
        board [0][0] = 'a';
        board [0][1] = 'b';
        board [0][2] = 'c';
        board [1][0] = 'd';
        board [1][1] = 'e';
        board [1][2] = 'f';
        board [2][0] = 'g';
        board [2][1] = 'h';
        board [2][2] = 'i';
       
}
Game.cpp (Driver)
#include "GameBoard.h"
#include <iostream>

using namespace std;

int main()
{
        cout << "Let's play Tic-Tac-Toe!!" << endl <<
                        "Player 1 gamepiece is X" << endl <<
                        "Player 2 gamepiece is O" << endl <<
                        endl << "Here's how the board is laid out" << endl << endl <<
                        "a|b|c" << endl << "-----" << endl <<
                        "d|e|f" << endl << "-----" << endl <<
                        "g|h|i" << endl << endl <<
                        "Let's begin..." << endl;

        //cout << GameBoard.board[0][0]; // illegal
        //cout << GameBoard::GameBoard.board[0][0]; //illegal

        return 0;
}
GameBoard.h
#ifndef GAME_H
#define GAME_H

#include <iostream>
using namespace std;

class GameBoard
{
        private:
                char a,b,c,d,e,f,g,h,i;

        public:
                GameBoard(); // default constructor to set gameboard to all blanks.
                /*playerChoice(char, char); // player's choice (X or O, location on brd)
                int isWin(); // displays a winner message
                printBoard(ostream &); // displays the current board to screen*/

};




#endif

What I am trying to do right now is be able to display my array using the class. I tried some of those but they were illegal declarations.

Narue Nov 21st, 2008 10:26 pm
Re: Using 2D Arrays inside Classes
 
Um, board isn't a member of the GameBoard class. It's a global variable.

MylesDBaker Nov 21st, 2008 10:27 pm
Re: Using 2D Arrays inside Classes
 
Okay I figured out the default constructor. Now I need to create a function named "playerChoice" which changes the stored array value into whatever the player's space that is chosen and replaces it with an 'X' or 'O' and then calls a function to print out the current board status.

jazzyangelz Nov 22nd, 2008 12:34 am
Re: Using 2D Arrays inside Classes
 
Hey I think your one of your functions is incorrect are you sure its not like this?

ostream&printBoard(ostream&);

cikara21 Nov 22nd, 2008 1:52 am
Re: Using 2D Arrays inside Classes
 
If you want to sharing your global variables, use 'static' on them..
Example:
static int myArray[2][2];

ArkM Nov 22nd, 2008 1:53 am
Re: Using 2D Arrays inside Classes
 
Quote:

Originally Posted by MylesDBaker (Post 741808)
Okay I figured out the default constructor. Now I need to create a function named "playerChoice" which changes the stored array value into whatever the player's space that is chosen and replaces it with an 'X' or 'O' and then calls a function to print out the current board status.

Not really? And what's a problem? Did you exhaust your C++ compiler member functions limit?..


All times are GMT -4. The time now is 11:24 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC