Using 2D Arrays inside Classes

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 30
Reputation: MylesDBaker is an unknown quantity at this point 
Solved Threads: 1
MylesDBaker MylesDBaker is offline Offline
Light Poster

Using 2D Arrays inside Classes

 
0
  #1
Nov 21st, 2008
Hello, I am trying to declare a 2D array (3x3) inside of a class.

I have it set up like this:

GameBoard.cpp
  1. #include "GameBoard.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. char board [3][3];
  7.  
  8. GameBoard::GameBoard()
  9. {
  10. board [0][0] = 'a';
  11. board [0][1] = 'b';
  12. board [0][2] = 'c';
  13. board [1][0] = 'd';
  14. board [1][1] = 'e';
  15. board [1][2] = 'f';
  16. board [2][0] = 'g';
  17. board [2][1] = 'h';
  18. board [2][2] = 'i';
  19.  
  20. }
Game.cpp (Driver)
  1. #include "GameBoard.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. cout << "Let's play Tic-Tac-Toe!!" << endl <<
  9. "Player 1 gamepiece is X" << endl <<
  10. "Player 2 gamepiece is O" << endl <<
  11. endl << "Here's how the board is laid out" << endl << endl <<
  12. "a|b|c" << endl << "-----" << endl <<
  13. "d|e|f" << endl << "-----" << endl <<
  14. "g|h|i" << endl << endl <<
  15. "Let's begin..." << endl;
  16.  
  17. //cout << GameBoard.board[0][0]; // illegal
  18. //cout << GameBoard::GameBoard.board[0][0]; //illegal
  19.  
  20. return 0;
  21. }
GameBoard.h
  1. #ifndef GAME_H
  2. #define GAME_H
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. class GameBoard
  8. {
  9. private:
  10. char a,b,c,d,e,f,g,h,i;
  11.  
  12. public:
  13. GameBoard(); // default constructor to set gameboard to all blanks.
  14. /*playerChoice(char, char); // player's choice (X or O, location on brd)
  15. int isWin(); // displays a winner message
  16. printBoard(ostream &); // displays the current board to screen*/
  17.  
  18. };
  19.  
  20.  
  21.  
  22.  
  23. #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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,624
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Using 2D Arrays inside Classes

 
0
  #2
Nov 21st, 2008
Um, board isn't a member of the GameBoard class. It's a global variable.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 30
Reputation: MylesDBaker is an unknown quantity at this point 
Solved Threads: 1
MylesDBaker MylesDBaker is offline Offline
Light Poster

Re: Using 2D Arrays inside Classes

 
0
  #3
Nov 21st, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: jazzyangelz is an unknown quantity at this point 
Solved Threads: 0
jazzyangelz jazzyangelz is offline Offline
Newbie Poster

Re: Using 2D Arrays inside Classes

 
0
  #4
Nov 22nd, 2008
Hey I think your one of your functions is incorrect are you sure its not like this?

ostream&printBoard(ostream&);
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Using 2D Arrays inside Classes

 
0
  #5
Nov 22nd, 2008
If you want to sharing your global variables, use 'static' on them..
Example:
  1. static int myArray[2][2];
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Using 2D Arrays inside Classes

 
0
  #6
Nov 22nd, 2008
Originally Posted by MylesDBaker View Post
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?..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC