| | |
Using 2D Arrays inside Classes
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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...
Views: 526 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






