Interaction between two classes

I have two classes and I need to pass one class by reference to the other 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
#ifndef PLAYER_H
#define PLAYER_H

#include<iostream>
#include<cmath>
#include<string>

class TicTacToe;

class Player
{
	public:
		Player(char name[], int index);
	
		int GetIndex();
	
		char* GetName();
		
		void NextMove(TicTacToe & board);
	
	private:
		char PlayerName[15];
		int PlayerId;
		int TotalMoves;
};

#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
#include "player.h"

	Player::Player(char name[], int index)
	{
	strcpy(PlayerName, name);
	PlayerId = index;
	}
	
	int Player::GetIndex()
	{
		return PlayerId;
	}
	
	char* Player::GetName()
	{
		return PlayerName;
	}
	
	void Player::NextMove(TicTacToe & board)
	{	
		int i=0, row=-1, col=-1;
		char move[3];
		
		while(PlayerName[i] != '\0')
		{
			cout << PlayerName[i];
			i++;
		}
		
		cout << ", make your move: ";
		cin >> row >> col;
		
		while(!board.SetValue(row, col, PlayerId))
		{
			cout << endl << "Illegal move, make another one: ";
			cin >> row >> col;
		}
			
		
	
	}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class TicTacToe
{

	private:
		char board[3][3];
		int GameStatus;
		int TotalMoves;
	
	public:

	TicTacToe();
	
	bool SetValue(int row, int column, int player);
	
	int GetStatus();

};

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
#include "tic_tac_toe.h"

	TicTacToe::TicTacToe()
	{
		for(int row = 0; row < 3; row++)
			for(int col = 0; col < 3; col ++)
				board[row][col] = '\0';
		
		TotalMoves = 0;
	}
	
	bool TicTacToe::SetValue(int row, int column, int player)
	{
		row--;
		column--;
		
		cout << endl << row <<" "<< column;
		if( row>2 || row<0 || column>2 || column<0)
			return false;
		
		if(board[row][column] == '\0' || board[row][column] == 'X' || board[row][column] == 'O')
			return false;
		
		if(player == 1)
			board[row][column] = 'X';
		else
			board[row][column] = 'O';
		
		TotalMoves++;
		return true;
		
	}


I receive the following error on compile:

player.cpp: In method `void Player::NextMove(class TicTacToe &)':
player.cpp:33: no matching function for call to `TicTacToe::SetValue (int &, int
&, int &)'
Last edited on
Why is the error saying the function takes three int references when both your header and the source file have the function taking normal ints?
I see no reason why that should be occurring, I think you must have miscopied the code or something, usually those kinds of errors result from inconsistent declarations of the functions.
I've copied the code exactly as I have it saved. I 've been messing around with it for a hour or two now and always end up with the same error. The following driver file works fine if the code that throws the error is commented out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include "tic_tac_toe.h"
#include "player.h"
#include <new>

using namespace std;

int main()
{
char name[5];
TicTacToe * test = new TicTacToe;
Player * test2 = new Player(name,1);

test2->NextMove(*test);
cout << test->SetValue(3,3,4);

}
Last edited on
Doesn't Player.cpp need to include tic_tac_toe.h ??
That was the problem, thanks for your help.
Topic archived. No new replies allowed.