Simple class to main code error or so I think

I can not find an example in my teachers notes or any examples on this forum to help me with the problem with private members in a class. I know nothing can mess with the member but I thought you could at least view the value (if its holding one) by calling it from another variable that has the same value derived from that private member function.

This is in the header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// This is the specification for the minesweeper class
const int SIZE = 3;
class minesweeper
{
	public:
		// Constructors
		minesweeper();
		minesweeper(int mines);
		void resetGame();
		// PRE: None
		// POST: The game is reset using the existing number of mines
		// All cells are cleared and the mines are placed in random cells
		// Adjacency values are calculated for all cells
		int flagCell(int x, int y);
		// PRE: x,y must be a valid row,column pair (between 1 and 3 inclusive)
		// POST: Cell x,y is flagged as a mine.
		// Function return value is the result of checkEndOfGame.
		void unflagCell(int x, int y);
		// PRE: x,y must be a valid row,column pair (between 1 and 3 inclusive)
		// POST: Cell x,y no longer flagged as a mine.
		int uncoverCell(int x, int y);
		// PRE: x,y must be a valid row,column pair (between 1 and 3 inclusive)
		// POST: Cell x,y is uncovered.
		// Function return value is the result of checkEndOfGame.
		void displayBoard();
		// PRE: None
		// POST: The board is displayed
		void revealBoard();
		// PRE: None
		// POST: The board is displayed with all cells uncovered.
		// The state of the board and the game are unchanged
	private:
		struct cell
		{
			bool mine; // Is there a mine in this cell
			bool covered; // Is this cell still covered
			int minesAdj; // How many mines are adjacent to this cell
			bool flagged; // Is this cell marked as a mine
		};
		cell board[SIZE][SIZE]; // The board
		int mineCount; // The number of mines
		// Private member functions used within the class
		int checkEndOfGame();
		// POST: This is used by uncoverCell and flagCell to check
		// for a win or loss after uncovering or flagging a cell.
		// Function return values are: 0 = keep playing, 1 = loss, 2 = win
		void clearAround(int i, int j);
		// POST: All cells adjacent to cell i,j that have an
		// adjacency value of zero are marked as uncovered
		// This is used by uncoverCell to speed up play.
};


This is the driver file or source file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "minesweeper.h"
#include <iostream>
#include <ctime>     
#include <cstdlib>

using namespace std;

minesweeper::minesweeper()
{
   mineCount = 0;
   resetGame();
}

minesweeper::minesweeper(int mines)
{
   mineCount = mines;
   resetGame();
}

void minesweeper::resetGame()
{
   
   for(int i = 0; i < SIZE; i++)
    {
       for(int j = 0; j < SIZE; j++)
	{   
           board[i][j].mine = 0;
           board[i][j].covered = 0;
           board[i][j].minesAdj = 0;
           board[i][j].flagged = 0;
        }
   }
}

int minesweeper::flagCell(int x, int y)
{
   return 0;
}

void minesweeper::unflagCell(int x, int y)
{
}

int minesweeper::uncoverCell(int x, int y)
{
   return 0;
}

void minesweeper::displayBoard()
{
   char covered = 'c';
   char board[SIZE][SIZE];
	
   for(int i = 0; i < SIZE; i++)
   {
      cout << endl;
      for(int j = 0; j < SIZE; j++)
      {
         board[i][j] = covered;
    	 cout << board[i][j] << "  ";
      }			 
   }
   cout << endl;
}

void minesweeper::revealBoard()
{
}


This is in the main file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "minesweeper.h"
#include <iostream>

using namespace std;

int main()
{
  minesweeper();
  int numMines;
  minesweeper gamePlay;
  
  cout <<"Enter number of mines: ";
  cin >> numMines;
  
  gamePlay.mineCount = numMines;
  gamePlay.displayBoard();
  
  cout << gamePlay.mineCount <<endl;
  
  return 0;
}


I know there are a ton left to do but I want to get at least this to compile correctly. My error is that mineCount is not accessible in main(). I know there are tons of examples for the minesweeper program but they are so many way of doing it I can't figure it out.

Thanks so much in advance.
Last edited on
Private variables can only be accessed by functions within the class, so you'd have to have something like this stated in your class.

1
2
public:
int GetMines () {return mineCount;}


then use gamepPlay.GetMines() to get the number. I think that might work.
Last edited on
I have to keep the header file the same. It was given to me by my instructor so I can't add any functions within the class.
Ah ok, looking at the header file, you have this constructor:

minesweeper(int mines);

which translates to

1
2
3
4
5
minesweeper::minesweeper()
{
   mineCount = 0;
   resetGame();
}


so instead, you could load the amount of mines by getting the number input from the user and declaring
minesweeper(mines);
OK, well that constructor is the constructor with parameters but the one that ("translates to") is the default parameter. I tried calling the constructor with parameters in the main code but I still get the same error.
I was a little hasty answering that so I wasn't very specific, but did you try:

minesweeper gamePlay(mines);
Yeah I tried that too. Still get the same complier error.

P.S. I didn't mean to come across rude. Sorry if I offended you AdventWolf.
You can't do gamePlay.mineCount = numMines;

If you do minesweeper gamePlay(mines); instead, it will fix the problem, since the constructor will then do gamePlay.mineCount = numMines; for you.
Oh it's np man, none taken.
Thanks. I redesigned what I wanted to do and solved my problem.
Topic archived. No new replies allowed.