Hi all,
I'm having a lot of difficulty trying to get this program to compile. I'm pretty new to C++, but what I've come up with so far makes logical sense to me, and I don't understand what the message "Request for member 'displayBoard' in 'game', which is of non-class type 'TicTacToe ()(char, char)' " means.
Here's the code:

#include <iostream>
#include <cstring>
using namespace std;
 

 
class TicTacToe 
{	
public:	
	TicTacToe(char player, char board[]);
	void displayBoard();
	int getMove();

private:
	char board[9];	
	char player;
	char box;
	char X, O;

};

int main() // tests TicTacToe class
{
	cout << "Let's play a game of Tic Tac Toe!";
	int turns = 9;
	TicTacToe game(char X, char E); // creates game object with player X, char Array E

	
	while (turns > 0)
	{

		game->displayBoard();
		game.getMove();
		turns--;			// decrements turn count 
	}
	cout << "Game Over!";
	return 0;
}


	
void TicTacToe::displayBoard()	//displays board
{ 
	cout 
	<< board[0] << " | " << board[1] << " | " << board[2] << "\n" 
	<< board[3] << " | " << board[4] << " | " << board[5] << "\n" 
	<< board[6] << " | " << board[7] << " | " << board[8] << "\n"; 
}
	
	
int TicTacToe::getMove() // gets move and fills in grid
{
	cout << "Player " << player << ", please pick a number corresponding to the grid of available choices: \n";
	cin.get(box);
	if (isdigit(box))
	{
		if ((board[box] != X ) && (board[box] != O))
		{
			board[box] = player;	// box is claimed by current player
			if (player == X)		// if statement changes players
				player = O;
			else player = X;	
			
		}
		else
			cout << "This box is already taken, please choose another: ";

	}
	else 
		cout << "This is not a number! Please choose an available number from 1-9: ";
	return board[box];
}

I'm pretty sure this program is a little ways from being polished and complete, with loops not working as expected and whatnot, but without being able to compile it, I'm unable to see what changes I need to make to complete it. Some help would be much appreciated! Thanks!

Recommended Answers

All 9 Replies

The problem is that in this line:

TicTacToe game(char X, char E);

You cannot have the type again - it needs to be:

TicTacToe game(X, E);

However, now you will get a "X is not declared" error. You either need to make X public and use game.X or do something else.

Good luck,

Dave

First off, the line "TicTacToe game(char X,char E);" actually is interpreted by the compiler as a function declaration, so that is what the error message points. "game" is declared as function, as far as the compiler can tell. In any case, this is not valid syntax, as Dave pointed out. You cannot declare variables X and E in the constructor call (or in any function call).

Second, the call "game->displayBoard();" should be "game.displayBoard();" because game is an object (if it was correctly constructed) and not a pointer (that requires this "->" syntax, as dereferencing operator).

Third, your use of "char" is not proper to identify the player. Declaring two char variables called X and O does not make them hold the values 'X' and 'O'. So forget the line "char X, O;" in your class.

Fourth, you need some initialization of your class, you need an implementation for your constructor. I would suggest you initialize the values of "board" to its index characters, e.g., "board[0] = '0'; board[1] = '1'; ..". Then you need to offset the value you read as "box" such that a character '1' because an integer value 1, or you can make "box" an "int" type (that's much easier).

Finally, adding a function to check if someone won the game after each move would be nice.

Do 1-4 and you should be able to compile, or at least it will be improved quite a bit. But you're right, the logic is good, just a few technical issues to solve.

Thanks for the responses! They helped clear up my program a lot. I attempted to make all of the suggested changes, but now when I compile, I get this message: (I'm programming in xcode by the way)

(!) "TicTacToe::TicTacToe(char, char*)", referenced from:
_main in main.o
Symbol(s) not found
Collect2: ID returned 1 exit status

I think my biggest hurdle in this lab is understanding how the array is initialized within a constructor. Is the array initialized when the constructor is called? Or do you have to initialize it before calling the constructor? Please bear with me as I attempt to explain my understanding of what is going in my program:

-I have a constructor "TicTacToe(char player, char board[]); " which creates an object of the class TicTacToe when the argument of a char (X)
and a char array (board) is passed to it.
-In the main, when the constructor is called to make a new TicTacToe object named "game", I now have an object named "game".
-The while loop displays the "board" in its current state (starting out with car numbers only), and then sends the parameters of "game" to the
getMove() function, which uses the char parameter (X) and alters the char array "board".
-This continues until "turns" reaches 0.

The lab also stated that "char board[9];" and "char player;" be private. If that's the case, I'm not sure if I'm actually programming by those rules. Does this mean the char board is already initialized? And how would I initialize it with chars if it is private? If it is already initialized, is it possible to use that as a parameter when calling the constructor?

#include <iostream>
#include <cstring>
using namespace std;
 
class TicTacToe 
{	
public:	
	TicTacToe(char player, char board[]);
	void displayBoard();
	int getMove();
	char X, O;	
private:
	char board[9];	
	char player;
	char box;
 
};
 
int main() // tests TicTacToe class
{
	char player = 'X';
	char board[9] = {'1','2','3','4','5','6','7','8','9'};  // initializes array with char numbers
	int turns = 9;
 
	cout << "Let's play a game of Tic Tac Toe!";
	TicTacToe game(player, board); // creates game object with player X, char Array E
	while (turns > 0)
	{
		game.displayBoard();
		game.getMove();
		turns--;			// decrements turn count 
	}
	cout << "Game Over!";
	return 0;
}
 
void TicTacToe::displayBoard()	//displays board
{ 
	cout 
	<< board[0] << " | " << board[1] << " | " << board[2] << "\n" 
	<< board[3] << " | " << board[4] << " | " << board[5] << "\n" 
	<< board[6] << " | " << board[7] << " | " << board[8] << "\n"; 
}
 
int TicTacToe::getMove() // gets move and fills in grid
{
	cout << "Player " << player << ", please pick a number corresponding to the grid of available choices: \n";
	cin.get(box);
	if (isdigit(box))
	{
		if ((board[box] != X ) && (board[box] != O)) // if box is not already claimed by 'X' or 'O'
		{
			board[box] = player;	// box is claimed by current player
			if (player == X)		// if statement changes players
				player = O;
			else player = X;	
 
		}
		else
			cout << "This box is already taken, please choose another: ";
 
	}
	else 
		cout << "This is not a number! Please choose an available number from 1-9: ";
	return board[box];
}

Again, a huge thanks for taking time out and helping me figure this all out. It's an online course, and the teacher isn't too helpful when tons of questions are asked via instant messenger.

And daviddoria / mike_2000_17,
Thank you for your suggestions. mike, I didn't include a function to check if someone won, because it'll be a requirement for the next lab assignment. Also, I would like to get this program running before I make things more complicated!

Ok.. one very important thing that is missing from your understanding and I'm sure this will be a problem-solving revelation:

You implement the constructor to initialize the data members (including board).

Line 8 is just the declaration of the constructor, it does not do anything, YOU have to provide an implementation for it.
That is the error you are getting with compiling it. The compiler tells you that it cannot find the constructor. And all the questions you are asking are exactly about that. So you need to add:

TicTacToe::TicTacToe(char player_, char board_[]) {
  //put whatever initializing function you want there. like setting all the values of "board".
};

Also, I repeat make "box" an integer (int) and take this line "char X, O;" (line 11) out of there. And you forgot to change a few occurrences of X and O to 'X' and 'O'.

Finally! I've managed to get the program to run! With your advice Mike, a clear head, and a very close look at some textbook examples, it is now up and running. There are a lot of screwy problems to iron out as expected, but at least I can troubleshoot each one individually until it runs smoothly. That tip about constructors was a lifesaver!

I will post the program once its finished. Thanks again!

I'd advise against posting the entire code. People will just search Google, find this post and be lazy and just copy it!

I know I would. :P

djextreme is right, I'm not interested in seeing the final, working code (and probably none of the "advise-giving" posters are either), so it would only be of interest to those who are dishonest enough to be browsing the internet for a homework freebie.

DONE. After an entire weekend of programming, it's finished, and I have a much, much better understanding of everything now. I rewrote the code so that each private member had its own mutator and accessor, and then REWROTE it again, with the realization that member functions are able to manipulate those private members. WOW that was a long and hard lesson learned =__=

I'll refrain from posting the entire code next time as well. That is a very good point that I had overlooked in a panic to get this program running. In this particular case however, I had no clue as to what code was needed to be shown and what was considered irrelevant. Thanks again everyone!

Congrats!

When it comes to the initial errorneous code, you can post as much as you want to help us find the errors (as long as it is not way too big). Because if someone else comes and copy/paste it for his own assignment, he will get the bad grade he deserves.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.