Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Sir_Spritely

C++ classes problem/confusion

This topic is 5539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am not sure the setup I have is correct practise so please let me know if this is the incorrect way of going about doing this. I have just spent ten minutes thinking about how to explain what is going down in my program which I know is not a very good sign! What is happening is that the class which contains the main() function creates a Menu object and calls two Menu class methods which display the menu and react to the selection by executing the appropriate method from the class which contains the main() function in. My problem lies in the methods which are called and which are in the class also containing the main() function. Inside these methods I want to instantiate another object from another class but I just get the following error: - C:\Program Files\Microsoft Visual Studio\MyProjects\C++\TicTacToeV1\TicTacToeV1.cpp(102) : error C2228: left of ''.GameRules'' must have class/struct/union type If this "dodgy" explanation has left you needing a brain transplant like it has just left me then I also will post the code below.
//-----------------------------------------------------------------------------

// File: TicTacToeV1.h

//

// Desc: Contains function headers.

//

// Copyright (c) Paul Kirkbride. All rights reserved.

//-----------------------------------------------------------------------------

#ifndef TICTACTOEV1_H
#define TICTACTOEV1_H

class TicTacToeV1 
{
public:
	TicTacToeV1();
	~TicTacToeV1();
	static void OnePlayer();
	static void TwoPlayer();
	static void GameRules();
//	int main();

};


#endif
//-----------------------------------------------------------------------------

// File: TicTacToeV1.cpp

//

// Desc: This is the second version of my tic tac toe computer game.  This has

//       a more OO approach and thus features for advanced C++ functionality.

//       The main game loop is featured here, all other functions are called

//       from their relevant classes.

//

// Copyright (c) Paul Kirkbride. All rights reserved.

//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------

// Include Files 

//-----------------------------------------------------------------------------

#include "stdafx.h"
#include <iostream.h>
#include <stdlib.h>	

#include "TicTacToeV1.h"
#include "Menu.h"
#include "GameRules.h"





//-----------------------------------------------------------------------------

// Name: TicTacToe()

// Desc: TicTacToeV1 class constructor

//-----------------------------------------------------------------------------

TicTacToeV1::TicTacToeV1()
{
	// Initiliase here

}





//-----------------------------------------------------------------------------

// Name: ~TicTacToe()

// Desc: TicTacToeV1 class destructor

//-----------------------------------------------------------------------------

TicTacToeV1::~TicTacToeV1()
{
	// Closing down done here

}





//-----------------------------------------------------------------------------

// Name: main()

// Desc: The main function also containing the game loop

//-----------------------------------------------------------------------------

int /*TicTacToeV1::*/main()
{
	Menu gameMenu;
	gameMenu.displayMenu();
	gameMenu.getuserOption();

	return 0;
}





//-----------------------------------------------------------------------------

// Name: OnePlayer()

// Desc: This function contains the one plyer game.

//-----------------------------------------------------------------------------

void TicTacToeV1::OnePlayer()
{
	cout << " In OnePlayer function " <<endl;
}





//-----------------------------------------------------------------------------

// Name: TwoPlayer()

// Desc: This function contains the two player game.

//-----------------------------------------------------------------------------

void TicTacToeV1::TwoPlayer()
{
	cout << " In TwoPlayer function " <<endl;
}





//-----------------------------------------------------------------------------

// Name: GameRules()

// Desc: This function contains the game rules

//-----------------------------------------------------------------------------

void TicTacToeV1::GameRules()
{
	GameRules.GameRules TTTRules;
	TTTRules.DisplayRules();

	//	cout << " In GameRules function " <<endl;

}
//-----------------------------------------------------------------------------

// File: Menu.h

//

// Desc: This will implement the menu functionality.

//

// Copyright (c) Paul Kirkbride. All rights reserved.

//-----------------------------------------------------------------------------

#ifndef MENU_H
#define MENU_H

class Menu
{
public:
	Menu();
	~Menu();
	
	void displayMenu();
	int  getuserOption();

private:
	int userOption;
};

#endif
//-----------------------------------------------------------------------------

// File: Menu.cpp

//

// Desc: This will implement the menu functionality.

//

// Copyright (c) Paul Kirkbride. All rights reserved.

//-----------------------------------------------------------------------------



//-----------------------------------------------------------------------------

// Include Files 

//-----------------------------------------------------------------------------

#include "stdafx.h"
#include <iostream.h>
#include <stdlib.h>	

#include "Menu.h"
#include "TicTacToeV1.h"



//-----------------------------------------------------------------------------

// Name: Menu()

// Desc: Menu constructor

//-----------------------------------------------------------------------------

Menu::Menu()
{
	// Clear the screen here, find out how to do this in DOS later

}





//-----------------------------------------------------------------------------

// Name: ~Menu()

// Desc: Menu Destructor

//-----------------------------------------------------------------------------

Menu::~Menu()
{
	// Any clearup on the menu''s behalf goes here

}





//-----------------------------------------------------------------------------

// Name: displayMenu()

// Desc: Contains the implementation of the menu, displays the menu, prompts

//       the user, performs an error check and executes requested section.

//-----------------------------------------------------------------------------

void Menu::displayMenu()
{
	cout << "\n\n-------------------------------------------------------------" <<endl;
	cout << "     T I C  T A C  T O E V0.1 (c) Paul Kirkbride 2003        " <<endl;
	cout << "-------------------------------------------------------------" <<endl;
	cout << "                       G A M E  M E N U                      " <<endl;
	cout << "-------------------------------------------------------------" <<endl;
	cout << "1. Play a one player game with computer as opponent          " <<endl;
	cout << "2. Play a two player game against another human player       " <<endl;
	cout << "3. Read the rules of the game                                " <<endl;
	cout << "4. Exit the game and return to windows                       " <<endl;
}





//-----------------------------------------------------------------------------

// Name: getuserOption()

// Desc: Get the user option

//-----------------------------------------------------------------------------

int Menu::getuserOption()
{
	cout << "Please enter your choice: ";
	cin >> userOption;
	
	switch ( userOption )
	{
	case 1: TicTacToeV1::OnePlayer();
		break;
	case 2: TicTacToeV1::TwoPlayer();
		break;
	case 3: TicTacToeV1::GameRules();
		break;
	case 4: 
		return 0;
	}
}
//-----------------------------------------------------------------------------

// File: GameRules.h

//

// Desc: This will implement the instructions and game rules.

//

// Copyright (c) Paul Kirkbride. All rights reserved.

//-----------------------------------------------------------------------------

#ifndef GAMERULES_H
#define GAMERULES_H


class GameRules
{
public:
	GameRules();
	~GameRules();

	void DisplayRules();
	void clrscr();
};


#endif
//-----------------------------------------------------------------------------

// File: GameRules.cpp

//

// Desc: This will implement the actual gam instructions and rules.

//

// Copyright (c) Paul Kirkbride. All rights reserved.

//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------

// Include Files 

//-----------------------------------------------------------------------------

#include <stdafx.h>
#include <iostream.h>
#include <stdlib.h>	
#include <conio.h>

#include "GameRules.h"
#include "TicTacToeV1.h"







//-----------------------------------------------------------------------------

// Name: GameRules()

// Desc: GameRules constructor

//-----------------------------------------------------------------------------

GameRules::GameRules()
{
	// Initilization here

}





//-----------------------------------------------------------------------------

// Name: ~GameRules()

// Desc: GameRules destructor

//-----------------------------------------------------------------------------

GameRules::~GameRules()
{
	// Closing down here

}





//-----------------------------------------------------------------------------

// Name: DisplayRules()

// Desc: Main function of this section, which will clear the screen and display

//       the game rules

//-----------------------------------------------------------------------------

void GameRules::DisplayRules()
{
	void GameRules::clrscr(); // Clear the DOS screen ready to display the rules


	cout << "\n\nT I C  T A C  T O E V1.0  G A M E  R U L E S" <<endl;
	cout << "--------------------------------------------" <<endl;
	cout << "There are two sections of this game, giving the player the option of" <<endl;
	cout << "playing this game on their own against the computer or against" <<endl;
	cout << "another human opponent.  There is currently no scoring functionality" <<endl;
	cout << "but that might be implemented at a later date.  For those not familiar" <<endl;
	cout << "with Tic Tac Toe or Noughts and Crosses as it is known in various" <<endl;
	cout << "countries these simple rules will teach you this less than difficult game.\n\n" <<endl;
	cout << "The game will always decide who gets to put their marker on the" <<endl;
	cout << "gameboard first. This is done in a random fashion, the game grid" <<endl;
	cout << "is a simple grid 3 * 3 with the total number of nine slots.  Each" <<endl;
	cout << "play a marker is placed on the grid alternatately between players." <<endl;
	cout << "This is down until either the grid is full or a player has produced" <<endl;
	cout << "a winning line.  To win the game a player must have three of his" <<endl;
	cout << "markers in either a row, column or diaganol line.  Upon either the grid" <<endl;
	cout << "becoming full or a player winning the game will return to the menu screen.\n\n" <<endl;
	cout << "For further updates of this game including a DirectX version please contact me.\n\n";
}
Help very much appreciated and sorry if this post just appears as a complete state of madness. I have just totally confused myself. Paul.

Share this post


Link to post
Share on other sites
Advertisement
Yep. Total state of madness !
j/k

You wrote:

GameRules.GameRules


This is a syntax error. Why? Because this means "take the member called ''GameRules'' inside the object called ''GameRules''".

GameRules (the left one) is a class, not an object. You can access its (static) members using GameRules::member. However, since you''re simply trying to create an object of type GameRules, I advise you to write:

GameRules TTTRules;

instead of

GameRules.GameRules TTTRules;

ToohrVyk

Share this post


Link to post
Share on other sites
That was my bad for leaving it in there! I was trying something out of sheer stupidity

However even changing that leaves me in a mess, with the error: -

C:\Program Files\Microsoft Visual Studio\MyProjects\C++\TicTacToeV1\TicTacToeV1.cpp(102) : error C2146: syntax error : missing '';'' before identifier ''TTTRules''

Help still very much needed

Paul.

Share this post


Link to post
Share on other sites
Either you''re missing a semicolon, or you''re doing something wacky and the compiler thinks you need a semicolon. Repost the code where you changed it.

Share this post


Link to post
Share on other sites
I predict I am attempting something wacky

The following was changed,


//-----------------------------------------------------------------------------

// File: TicTacToeV1.cpp

//

// Desc: This is the second version of my tic tac toe computer game. This has

// a more OO approach and thus features for advanced C++ functionality.

// The main game loop is featured here, all other functions are called

// from their relevant classes.

//

// Copyright (c) Paul Kirkbride. All rights reserved.

//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------

// Include Files

//-----------------------------------------------------------------------------

#include "stdafx.h"
#include <iostream.h>
#include <stdlib.h>

#include "TicTacToeV1.h"
#include "Menu.h"
#include "GameRules.h"





//-----------------------------------------------------------------------------

// Name: TicTacToe()

// Desc: TicTacToeV1 class constructor

//-----------------------------------------------------------------------------

TicTacToeV1::TicTacToeV1()
{
// Initiliase here

}





//-----------------------------------------------------------------------------

// Name: ~TicTacToe()

// Desc: TicTacToeV1 class destructor

//-----------------------------------------------------------------------------

TicTacToeV1::~TicTacToeV1()
{
// Closing down done here

}





//-----------------------------------------------------------------------------

// Name: main()

// Desc: The main function also containing the game loop

//-----------------------------------------------------------------------------

int /*TicTacToeV1::*/main()
{
Menu gameMenu;
gameMenu.displayMenu();
gameMenu.getuserOption();

return 0;
}





//-----------------------------------------------------------------------------

// Name: OnePlayer()

// Desc: This function contains the one plyer game.

//-----------------------------------------------------------------------------

void TicTacToeV1::OnePlayer()
{
cout << " In OnePlayer function " <<endl;
}





//-----------------------------------------------------------------------------

// Name: TwoPlayer()

// Desc: This function contains the two player game.

//-----------------------------------------------------------------------------

void TicTacToeV1::TwoPlayer()
{
cout << " In TwoPlayer function " <<endl;
}





//-----------------------------------------------------------------------------

// Name: GameRules()

// Desc: This function contains the game rules

//-----------------------------------------------------------------------------

void TicTacToeV1::GameRules()
{
GameRules TTTRules;
TTTRules.DisplayRules();

// cout << " In GameRules function " <<endl;


Share this post


Link to post
Share on other sites
My guess is that the compiler interprets "GameRules" as the function GameRules, not the class GameRules. Try changing the function name.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!