Question on declaring members in classes


I'm having trouble understanding the rules on declaring members in classes.

I have initialised the char 'piece' in the classes constructor and included it as a private member in the class' header file. The program compiles and runs OK but when the function Player::print() is called there is a blank space where the 'piece' value should be.

Have I initialised the private member char 'piece' in the correct way? Looking at the tutorial on classes (http://www.cplusplus.com/doc/tutorial/classes/) I can't seem to spot the problem.

Any help would be great thanks.

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
//Player.cpp
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

#include "Player.h"

Player::Player(char humanPiece)
{
	piece = humanPiece;
}

void Player::print()
{
	cout << piece <<"'s to play." << endl;
}

char Player::playerMove()
{
	cout << "Choose square to claim: ";
	cin >> move;
	return move;
	
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Player.h
#ifndef PLAYER_H
#define PLAYER_H

class Player
{
public:
	Player(char);
	void print();
	char playerMove();
private:
	char piece;
	char move;
};

#endif 


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
//main.cpp
#include <iostream>
#include <string>
#include <cctype>

#include "Player.h"
#include "Computer.h"
#include "Gameboard.h"

using namespace std;

const char x = 'X';
const char o = 'O';
const char noOne = 'N';
const char tie = 'T';
void const instructions();
void pieceAsk();
char onePiece;
char compPiece;
char turn;
char winner;
char thisMove;
char test;


int main() 
{

	Gameboard board;
    instructions();
	board.printBoard();
	Player playerOne(onePiece);
	Computer comp(compPiece);
	pieceAsk();
	winner = noOne;
	turn = x;
	
	while(winner == noOne)
	{
	
		if(turn == onePiece)
		{
			playerOne.print();
			//thisMove = playerOne.playerMove();
			board.makeMove(playerOne.playerMove(), onePiece);
			board.printBoard();
			turn = compPiece;
			cin >> test;
			
		}
		else if(turn == compPiece)
		{
			comp.print();
			turn == onePiece;
		}
		else
		{
			printf("Something's gone wrong");
		}
	}
	
    return 0;
}

Can you tell me what onePiece is initialized as?
onePiece is a char initialised to either 'X' or 'O'...

heres the rest of the main functions...

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

void const instructions()
{
	cout << "\nXOXOXOX Noughts and Crosses Extreme XOXOXOX" << endl;
	cout << "\nRules: Match three in a row vertically, horizontally or diagonally\n"<<endl;
}

void pieceAsk()
{
	char choice;
	cout << "Would you like to be Os or Xs (Enter O or X): ";
	cin >> choice;
	
	while((choice != o) && (choice != x))
	{
		cout << "Please enter a valid answer (X or O): ";
		cin >> choice;
	}
		
	if(choice == x)
	{
		cout << "\nYou will play as X's\n";
		onePiece = x;
		compPiece = o;
	}
	else if(choice == o)
	{
		cout << "\nYou will play as O's\n";
		onePiece = o;
		compPiece = x;
	}
	else
	{
		cout << "\nERROR. DEFAULT SETTING: You will play as X's\n";
		onePiece = x;
		compPiece = o;
	}
}




Player playerOne(onePiece);

When that happens what is onePiece?

Remember computers execute tasks sequentially.

y=2;
x=y;
y=3;

x!=3;
I meant to say that x is not 3.
Ah. I see now... It works when I move pieceAsk() before initialising the class.

Thanks a lot for your help.
Your welcome.
Topic archived. No new replies allowed.