943,834 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 730
  • C++ RSS
Nov 21st, 2008
0

Using 2D Arrays inside Classes

Expand Post »
Hello, I am trying to declare a 2D array (3x3) inside of a class.

I have it set up like this:

GameBoard.cpp
C++ Syntax (Toggle Plain Text)
  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)
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
MylesDBaker is offline Offline
36 posts
since Sep 2008
Nov 21st, 2008
1

Re: Using 2D Arrays inside Classes

Um, board isn't a member of the GameBoard class. It's a global variable.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 21st, 2008
0

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
MylesDBaker is offline Offline
36 posts
since Sep 2008
Nov 22nd, 2008
0

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&);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jazzyangelz is offline Offline
9 posts
since Oct 2008
Nov 22nd, 2008
0

Re: Using 2D Arrays inside Classes

If you want to sharing your global variables, use 'static' on them..
Example:
c++ Syntax (Toggle Plain Text)
  1. static int myArray[2][2];
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Nov 22nd, 2008
0

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.
Not really? And what's a problem? Did you exhaust your C++ compiler member functions limit?..
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: hpp?
Next Thread in C++ Forum Timeline: What header do I look at for...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC