Hello,

I am working on two separate programs because I was trying one differently. I got my other program’s issue solved but now I would like to complete this one. But I am little lost on the error because the data it is complaining about is part of the class. I have included my header file code for verification.

The error is:
(126) : error C2677: binary '==' : no global operator found which takes type 'DiceGame:: Status' (or there is no acceptable conversion)

My header file:

// Craps class definition
class Dice
{
public:
	int rollDice();				// function that rolls the dice and calculates the sum	
};
class DiceGame
{
public:
	void play();				// function play controls the details of the game
	void chatter();
	static bool test();
private:	
	enum Status { CONTINUE, WON, LOST }; // enumeration with constants that represent the game status - all caps in constants
	int myPoint; // point if no win or loss on first roll
	Status gameStatus; // can contain CONTINUE, WON or LOST
};

class Wagering
{
public:
	static bool wageringTest(double, double);

};

My code is the following:

#include "Craps.h"			// include definition of class Craps

// function that rolls the dice and calculates the sum	
int Dice::rollDice()
{
	// randomize random number generator using current time
	srand ( (unsigned)time ( 0 ) );	
	
	// pick random die values
	int die1 = 1 + rand() % 6; // first die roll
	int die2 = 1 + rand() % 6; // second die roll

	int sum = die1 + die2; // compute sum of die values

	// display results of this roll
	cout << "Player rolled " << die1 << " + " << die2 << " = " << sum << endl;
	return sum; // end function rollDice
} // end function rollDice

// function play will control the details of the game
void DiceGame::play()
{
	Dice rollDice; 
	int sumOfDice = rollDice.rollDice(); // first roll of the dice

	// determine game status and point (if needed) based on first roll
	switch ( sumOfDice )
	{
		case 7: // win with 7 on first roll
		case 11: // win with 11 on first roll
			gameStatus = WON;
			break;
		case 2: // lose with 2 on first roll
		case 3: // lose with 3 on first roll
		case 12: // lose with 12 on first roll
			gameStatus = LOST;
			break;
		default: // did not win or lose, so remember point
			gameStatus = CONTINUE; // game is not over
			myPoint = sumOfDice; // remember the point
			cout << "Point is " << myPoint << endl;
			break; // optional at end of switch
	} // end switch

	// while game is not complete
	while ( gameStatus == CONTINUE ) // not WON or LOST
	{
		chatter();
		sumOfDice = rollDice.rollDice(); // roll dice again

	  // determine game status
	  if (sumOfDice == myPoint) // win by making point
		gameStatus = WON;
	  else
	  if (sumOfDice == 7) // lose by rolling 7 before point
	     gameStatus = LOST;
} // end while
}
// chatter displays messages at random to create "chatter"
void DiceGame::chatter()
{
   // choose message at random
   switch ( rand() % 9 ) 
   {
      case 0:
         cout << "Oh, you're going for broke, huh?";
         break;
      case 1:
         cout << "Aw cmon, take a chance!";
         break;
      case 2:
         cout << "Hey, I think this guy is going to break the bank!!";
         break;
      case 3:
         cout << "You're up big. Now's the time to cash in your chips!";
         break;
      case 4:
         cout << "Way too lucky! Those dice have to be loaded!";
         break;
      case 5:
         cout << "Bet it all! Bet it all!";
         break;
      case 6:
         cout << "Can I borrow a chip?";
         break;
      case 7:
         cout << "Let's try our luck at another table.";
         break;
      case 8:
         cout << "You're a cheat! It is just a matter of time"
            << "\nbefore I catch you!!!";
         break;
   } // end switch

   cout << endl;
} // end function chatter

bool DiceGame::test()
{
   double wager = 100.00; // wager for current game
   double bankBalance = 1000.00; // current bank balance
   int games = 20;

   do
   {
	   DiceGame roll; // create object
	   roll.play();

	   // display won or lost message
		if( gameStatus == WON )
		{
			bankBalance += wager;
			cout << "\nPlayer wins. Your new balance is $" << bankBalance << endl;
		}
		else
		{
			bankBalance -= wager;

			if(bankBalance <= 0)
			{
				cout << "\nSorry. You busted!\n";
			}
			else
			{
				cout << "\nPlayer loses. Your new balance is $" << bankBalance << endl;
			}
		}

	   //roll.displayMessage();
	   roll.chatter();
	   if(Wagering::wageringTest(wager, bankBalance))
	   {
		   games--;
	   }
	   else
	   {
		  games = 0;	// to terminate the loop and exit
		  cout << "Game over Wager or balance issue Game terminated.";
	   }

   } while ( games > 0 );

	return true;
}
bool Wagering::wageringTest(double currentWager, double currentBankBalance)
{
	if (currentBankBalance >= 0)
	{
		if(currentWager <= currentBankBalance)
		{
			return true;
		}	
		else
		{
			return false;
		}
	}
	else 
	{
		return false;
	}
}

Where is 'Status' defined?? .. Basically 'Status' is not a native Datatype of c++ and so it doesnt have a predefined '==' operator for it. So you need to overload the particular operator before you do something like Status == xxx.

Read more about the topic "Operator Overloading" and you will understand.

Change line 141 from : if( gameStatus == WON ) to: if( roll.gameStatus == WON ) And your problem should be solved

OK my bad.. i didnt see that "Status" is declared right above in the class defintion and it's an Enum. Please ignore my post. Though you can still read about 'operator overloading' if you feel like.

sorry for the confusion

Change line 141 from : if( gameStatus == WON ) to: if( roll.gameStatus == WON ) And your problem should be solved

Thanks that worked but now I am having an issue with the random number generator. It seems like it's not working, because the dice results stay the same and the chatter comments also stays at the same case.

my code is:

// Exercise 6.54 Solution: Craps.cpp - Craps simulation with wagering and chatter
// Member-function definitions for Craps

// allows program to perform input and output; the program uses the following
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <cstdlib> // contains prototypes for functions srand and rand
using std::rand;
using std::srand;

#include <ctime> // contains prototype for function time
using std::time;      

#include "Craps.h"			// include definition of class Craps

Dice::Dice()
{
	// randomize random number generator using current time
	srand ( (unsigned)time ( 0 ) );	// initialize the random number generator
}
// function that rolls the dice and calculates the sum	
int Dice::rollDice()
{
	//Dice();

	int die1 = 1 + rand() % 6; // first die roll
	int die2 = 1 + rand() % 6; // second die roll

	int sum = die1 + die2; // compute sum of die values

	// display results of this roll
	cout << "Player rolled " << die1 << " + " << die2 << " = " << sum << endl;
	return sum; // end function rollDice
} // end function rollDice

// function play will control the details of the game
void DiceGame::play()
{
	Dice rollDice; 
	int sumOfDice = rollDice.rollDice(); // first roll of the dice

	// determine game status and point (if needed) based on first roll
	switch ( sumOfDice )
	{
		case 7: // win with 7 on first roll
		case 11: // win with 11 on first roll
			gameStatus = WON;
			break;
		case 2: // lose with 2 on first roll
		case 3: // lose with 3 on first roll
		case 12: // lose with 12 on first roll
			gameStatus = LOST;
			break;
		default: // did not win or lose, so remember point
			gameStatus = CONTINUE; // game is not over
			myPoint = sumOfDice; // remember the point
			cout << "Point is " << myPoint << endl;
			break; // optional at end of switch
	} // end switch

	// while game is not complete
	while ( gameStatus == CONTINUE ) // not WON or LOST
	{
		chatter();
		sumOfDice = rollDice.rollDice(); // roll dice again

	  // determine game status
	  if (sumOfDice == myPoint) // win by making point
		gameStatus = WON;
	  else
	  if (sumOfDice == 7) // lose by rolling 7 before point
	     gameStatus = LOST;
} // end while
}
// chatter displays messages at random to create "chatter"
void DiceGame::chatter()
{
   // choose message at random
   switch ( rand() % 9 ) 
   {
      case 0:
         cout << "Oh, you're going for broke, huh?";
         break;
      case 1:
         cout << "Aw cmon, take a chance!";
         break;
      case 2:
         cout << "Hey, I think this guy is going to break the bank!!";
         break;
      case 3:
         cout << "You're up big. Now's the time to cash in your chips!";
         break;
      case 4:
         cout << "Way too lucky! Those dice have to be loaded!";
         break;
      case 5:
         cout << "Bet it all! Bet it all!";
         break;
      case 6:
         cout << "Can I borrow a chip?";
         break;
      case 7:
         cout << "Let's try our luck at another table.";
         break;
      case 8:
         cout << "You're a cheat! It is just a matter of time before I catch you!!!";
         break;
   } // end switch

   cout << endl;
} // end function chatter

bool DiceGame::test()
{
   double wager = 100.00; // wager for current game
   double bankBalance = 1000.00; // current bank balance
   int games = 20;

   cout << "\nYou have $" << bankBalance << " in the bank.\n";
   
   do
   {
	   DiceGame roll; // create object
	   roll.play();

	   // display won or lost message
		if( roll.gameStatus == WON )
		{
			bankBalance += wager;
			cout << "\nPlayer wins. Your new balance is $" << bankBalance << endl;
		}
		else
		{
			bankBalance -= wager;

			if(bankBalance <= 0)
			{
				cout << "\nSorry. You busted!\n";
			}
			else
			{
				cout << "\nPlayer loses. Your new balance is $" << bankBalance << endl;
			}
		}

	   //roll.displayMessage();
	   roll.chatter();
	   if(Wagering::wageringTest(wager, bankBalance))
	   {
		   games--;
	   }
	   else
	   {
		  games = 0;	// to terminate the loop and exit
		  cout << "Game over Wager or balance issue Game terminated.";
	   }

   } while ( games > 0 );

	return true;
}
bool Wagering::wageringTest(double currentWager, double currentBankBalance)
{
	if (currentBankBalance >= 0)
	{
		if(currentWager <= currentBankBalance)
		{
			return true;
		}	
		else
		{
			return false;
		}
	}
	else 
	{
		return false;
	}
}

This: srand ( (unsigned)time ( 0 ) ); should be in main() as on of the first lines, not in the constructor.

This: srand ( (unsigned)time ( 0 ) ); should be in main() as on of the first lines, not in the constructor.

thank you :)

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.