Skip to main content
gamedev.net
🔒 Locked

Problem with File Includes

Started by UBC_Wiskatos 5 replies 400+ views
Original Post
UBC_Wiskatos
UBC_Wiskatos
Hi, I'm making a Tic-Tac-Toe game, but I'm having problems with the file includes. The compiler error stumps me, because it goes away if I put all the files into one big file. Here are my files: Main.cpp
    
#include "Game.h"
#include "Board.h"

int main()
{
	CBoard theBoard;
	//	CPlayer Player[2];

	CGame theGame(&theBoard);
	return 0;
}
  
  
Game.h
  
      
#ifndef GAME_H
#define GAME_H

/**********************************************
Class: Game
Author: UBC_Wiskatos
Function: Controls the whole game (input, etc.)
**********************************************/

class CGame
{
public:
	CGame();
	CGame(CBoard *pBoard);		// Main constructor

	~CGame();					// Main destructor

private:
};

#endif
  
  
Board.h
  
      
#ifndef BOARD_H
#define BOARD_H

class CBoard
{
public:
	CBoard();					// Main constructor

	~CBoard();					// Main destructor

	void Draw();				// Draws the board

private:
	int m_board[3][3];
	int m_rows;
	int m_cols;
};

#endif
  
  
Board.cpp
  
      
#include "Board.h"
#include <iostream>

using namespace std;

CBoard::CBoard()
{
	// Set the number of rows and columns

	m_rows = 3;
	m_cols = 3;

	// Clear the screen

	system("cls");

	// Set the array cells to 0

	for(int y = 0; y < m_rows; y++)
	{
		for(int x = 0; x < m_cols; x++)
		{
			m_board[x][y] = 0;
		}
	}

	// Draw a blank board

	Draw();

m_board[1][1] = 2;
}

CBoard::~CBoard()
{
}

void CBoard::Draw()
{
	// Clear the screen

	system("cls");

	// Display our heading

	cout << "TIC-TAC-TOE" << endl << endl;

	// Loop through the rows

	for(int y = 0; y < m_rows; y++)
	{
		// Loop through the columns

		for(int x = 0; x < m_cols; x++)
		{
			// Add a blank space in front of character

			cout << " ";
			
			// If we find a 1, put an X, if we find a 2,

			// put an O, if neither, put a space

			switch(m_board[x][y])
			{
				case 1:
					cout << "X";
					break;
				case 2:
					cout << "0";
					break;
				default:
					cout << " ";
			}

			// Add a blank space after character

			cout << " ";
			
			// If we are not on the last column, put a #

			if(x < m_cols - 1)
			{
				cout << "#";
			}
		}

		// Finish up the line

		cout << endl;
	
		// If we are not on the last row, put the line

		if(y < m_rows - 1)
		{
			// Loop through the number of columns

			for(int i = 0; i < m_cols; i++)
			{
				// For each column, put ###

				cout << "###";

				// If we are not on the last column, add an extra #

				// for the dividers that split columns

				if(i < m_cols - 1)
				{
					cout << "#";
				}
			}

			// Finish up the line

			cout << endl;
		}
	}
}
  
  
Game.cpp
  
      
#include "Game.h"

CGame::CGame()
{
}

CGame::CGame(CBoard *pBoard)
{
	pBoard->Draw();
}

CGame::~CGame()
{
}
    
The error given is: c:\mfc testing\tic tac toe\game.h(14) : error C2629: unexpected 'class CGame (' c:\mfc testing\tic tac toe\game.h(14) : error C2238: unexpected token(s) preceding ';' And some others. These are the main ones I cannot solve. [edited by - UBC_Wiskatos on August 3, 2002 6:15:54 PM]
[email=ubc_wiskatos@hotmail.com" target="_blank" style="width: 10px; height: 10px; background: #fe7a21; overflow: hidden; display: block; margin-bottom: 2px;][/email]
VolkerG
VolkerG
You didn''t post the original Game.h file, did you. I ask since I can''t find anything in line 14 and that''s where the error should be.
UBC_Wiskatos
UBC_Wiskatos
I took out the header comments, that is all. I didn''t think this would be relevant because it can be seen that line 14 is where the class prototype begins (it is placed in quotation marks).
[email=ubc_wiskatos@hotmail.com" target="_blank" style="width: 10px; height: 10px; background: #fe7a21; overflow: hidden; display: block; margin-bottom: 2px;][/email]
Beer Hunter
Beer Hunter
game.h needs to know that CBoard is a type.

In game.h, before "class CGame", add the line "class CBoard;" (note the semicolon).
In game.cpp, include board.h.

I''m not sure if that''s the only error - but give it a try.
UBC_Wiskatos
UBC_Wiskatos
Wow, thank you very much! All the errors are gone now. Related to this, it seems my different classes will get messy after a while if I have to remind each one what type things are, and include various other headers. Could someone point me to some kind of better way to manage classes (maybe some kind of superheader?) so they know about each other, rather than having to custom-tailor includes and "class CBlah" things for every class.

Could this be done by doing a big header and then having each statement surrounded by #ifndef, #define, #endif? Say...

#ifndef CBOARD
#define CBOARD
class CBoard;
#endif

Thanks!
[email=ubc_wiskatos@hotmail.com" target="_blank" style="width: 10px; height: 10px; background: #fe7a21; overflow: hidden; display: block; margin-bottom: 2px;][/email]
UBC_Wiskatos
UBC_Wiskatos
That is... interesting. However, it doesn''t really answer my question. Is this chicken OOP or KFC?
[email=ubc_wiskatos@hotmail.com" target="_blank" style="width: 10px; height: 10px; background: #fe7a21; overflow: hidden; display: block; margin-bottom: 2px;][/email]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.