| | |
Using 2D Arrays inside Classes
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 30
Reputation:
Solved Threads: 1
Hello, I am trying to declare a 2D array (3x3) inside of a class.
I have it set up like this:
GameBoard.cpp Game.cpp (Driver)
GameBoard.h
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.
I have it set up like this:
GameBoard.cpp
C++ Syntax (Toggle Plain Text)
#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'; }
C++ Syntax (Toggle Plain Text)
#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; }
C++ Syntax (Toggle Plain Text)
#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.
If you want to sharing your global variables, use 'static' on them..
Example:
Example:
c++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- First Attempt at Arrays (Java)
- Javascript Structures? (JavaScript / DHTML / AJAX)
- How do I place images on a rotating divided cube using Java? (Java)
- Total Newbie Could Use A Pointer In The Right Direction (C++)
- Why Data Structures???...QUESTIONS INSIDE (C++)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- Help Me Please (C++)
Other Threads in the C++ Forum
- Previous Thread: hpp?
- Next Thread: What header do I look at for...
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






