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.

Recommended Answers

All 5 Replies

Um, board isn't a member of the GameBoard class. It's a global variable.

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.

Hey I think your one of your functions is incorrect are you sure its not like this?

ostream&printBoard(ostream&);

If you want to sharing your global variables, use 'static' on them..
Example:

static int myArray[2][2];

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?..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.