calling a function from one class to another

I am trying to make the game tic tac toe. I am suppose to have two classes one called board where the board is created and the moves are made. The other class is called TicTacToe where it loops and checks to see who won.

I am trying to figure out how to print the game board with in my play function in TicTacToe class.

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
  #include <iostream>

class Board
{
    public:
	void print(){
		char arrayBoard[3][3] = { { '.', '.', '.' },
	{ '.', '.', '.' },
	{ '.', '.', '.' } };

	std::cout << " 123\n";
	for (int i = 0; i < 3; i++) {

		std::cout << i + 1 << arrayBoard[i][0] << arrayBoard[i][1] << arrayBoard[i][2] << std::endl;
	}
};

class TicTacToe
{
    public:
	void play();
};

void TicTacToe::play()	
	{
                //call class Board and print the board
		Board::print();
                 // play the game here
	}


int main()
{
	TicTacToe::print();
	return 0;
}
From what I understand, your Board::print(); is proper, but certain IDEs and compilers only interpret certain ways of calling functions within classes. If you are using Visual Studio, my understanding is that VS only recognizes
Board->print();. As far as telling the IDE that the certain function is part of the class, '::' is proper. You're good there.

Secondly, you're coding in a function "play()" outside of the TicTacToe class that's already created inside the TicTacToe class. Why don't you just code it within that function? It's a little redundant, isn't it?
Last edited on
Well there's a couple of thing I'd like to comment on.

Firstly, the design is not something to be admired. But besides that:

1) In your class Board, there is a closing curly bracket missing '}'. Fix that first. :)

2) Board::print() This is a call to a static function. If you really mustn't create an object and call the function through the object, then you'd better declare print() to be static!

3) In the main function you call TicTacToe::print(). Now this would never work, unless TicTacToe derives from Board and print() is a static function. I think you meant to call TicTacTo::play(). That too would be a static function call, so you should know what to do.
Last edited on
Thank you so much WakeofMisery and sasauke! I was able to get it working
Side note the reason why I called the function "play()" out side the class is because the homework calls for an hpp and a cpp 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
 #include <iostream>

class Board
{
    public:
	static void print();
};
	
	
void Board::print()	
{
		char arrayBoard[3][3] = { { '.', '.', '.' },
	                            { '.', '.', '.' },
	                            { '.', '.', '.' } };

	std::cout << " 123\n";
	for (int i = 0; i < 3; i++) 
	{
		std::cout << i + 1 << arrayBoard[i][0] << arrayBoard[i][1] << arrayBoard[i][2] << std::endl;
	}
}

class TicTacToe
{
    public:
	static void play();
};

void TicTacToe::play()	
	{
                //call class Board and print the board
		Board::print();
                 // play the game here
	}


int main()
{
    TicTacToe game;
	game.play();
	
	return 0;
}
I think you misunderstand.

In the main function, you create an object of TicTacToe game and you call play() from that object. In this case there is no need for play() to be static. A static member function is only used whenever you want to call it without an associated object of the class, as is the case with Board::print().
Topic archived. No new replies allowed.