error C2677: binary '==' : no global operator found

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2008
Posts: 96
Reputation: QuantNeeds is an unknown quantity at this point 
Solved Threads: 0
QuantNeeds QuantNeeds is offline Offline
Junior Poster in Training

error C2677: binary '==' : no global operator found

 
0
  #1
Jun 30th, 2008
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:
  1. // Craps class definition
  2. class Dice
  3. {
  4. public:
  5. int rollDice(); // function that rolls the dice and calculates the sum
  6. };
  7. class DiceGame
  8. {
  9. public:
  10. void play(); // function play controls the details of the game
  11. void chatter();
  12. static bool test();
  13. private:
  14. enum Status { CONTINUE, WON, LOST }; // enumeration with constants that represent the game status - all caps in constants
  15. int myPoint; // point if no win or loss on first roll
  16. Status gameStatus; // can contain CONTINUE, WON or LOST
  17. };
  18.  
  19. class Wagering
  20. {
  21. public:
  22. static bool wageringTest(double, double);
  23.  
  24. };

My code is the following:
  1. #include "Craps.h" // include definition of class Craps
  2.  
  3. // function that rolls the dice and calculates the sum
  4. int Dice::rollDice()
  5. {
  6. // randomize random number generator using current time
  7. srand ( (unsigned)time ( 0 ) );
  8.  
  9. // pick random die values
  10. int die1 = 1 + rand() % 6; // first die roll
  11. int die2 = 1 + rand() % 6; // second die roll
  12.  
  13. int sum = die1 + die2; // compute sum of die values
  14.  
  15. // display results of this roll
  16. cout << "Player rolled " << die1 << " + " << die2 << " = " << sum << endl;
  17. return sum; // end function rollDice
  18. } // end function rollDice
  19.  
  20. // function play will control the details of the game
  21. void DiceGame::play()
  22. {
  23. Dice rollDice;
  24. int sumOfDice = rollDice.rollDice(); // first roll of the dice
  25.  
  26. // determine game status and point (if needed) based on first roll
  27. switch ( sumOfDice )
  28. {
  29. case 7: // win with 7 on first roll
  30. case 11: // win with 11 on first roll
  31. gameStatus = WON;
  32. break;
  33. case 2: // lose with 2 on first roll
  34. case 3: // lose with 3 on first roll
  35. case 12: // lose with 12 on first roll
  36. gameStatus = LOST;
  37. break;
  38. default: // did not win or lose, so remember point
  39. gameStatus = CONTINUE; // game is not over
  40. myPoint = sumOfDice; // remember the point
  41. cout << "Point is " << myPoint << endl;
  42. break; // optional at end of switch
  43. } // end switch
  44.  
  45. // while game is not complete
  46. while ( gameStatus == CONTINUE ) // not WON or LOST
  47. {
  48. chatter();
  49. sumOfDice = rollDice.rollDice(); // roll dice again
  50.  
  51. // determine game status
  52. if (sumOfDice == myPoint) // win by making point
  53. gameStatus = WON;
  54. else
  55. if (sumOfDice == 7) // lose by rolling 7 before point
  56. gameStatus = LOST;
  57. } // end while
  58. }
  59. // chatter displays messages at random to create "chatter"
  60. void DiceGame::chatter()
  61. {
  62. // choose message at random
  63. switch ( rand() % 9 )
  64. {
  65. case 0:
  66. cout << "Oh, you're going for broke, huh?";
  67. break;
  68. case 1:
  69. cout << "Aw cmon, take a chance!";
  70. break;
  71. case 2:
  72. cout << "Hey, I think this guy is going to break the bank!!";
  73. break;
  74. case 3:
  75. cout << "You're up big. Now's the time to cash in your chips!";
  76. break;
  77. case 4:
  78. cout << "Way too lucky! Those dice have to be loaded!";
  79. break;
  80. case 5:
  81. cout << "Bet it all! Bet it all!";
  82. break;
  83. case 6:
  84. cout << "Can I borrow a chip?";
  85. break;
  86. case 7:
  87. cout << "Let's try our luck at another table.";
  88. break;
  89. case 8:
  90. cout << "You're a cheat! It is just a matter of time"
  91. << "\nbefore I catch you!!!";
  92. break;
  93. } // end switch
  94.  
  95. cout << endl;
  96. } // end function chatter
  97.  
  98. bool DiceGame::test()
  99. {
  100. double wager = 100.00; // wager for current game
  101. double bankBalance = 1000.00; // current bank balance
  102. int games = 20;
  103.  
  104. do
  105. {
  106. DiceGame roll; // create object
  107. roll.play();
  108.  
  109. // display won or lost message
  110. if( gameStatus == WON )
  111. {
  112. bankBalance += wager;
  113. cout << "\nPlayer wins. Your new balance is $" << bankBalance << endl;
  114. }
  115. else
  116. {
  117. bankBalance -= wager;
  118.  
  119. if(bankBalance <= 0)
  120. {
  121. cout << "\nSorry. You busted!\n";
  122. }
  123. else
  124. {
  125. cout << "\nPlayer loses. Your new balance is $" << bankBalance << endl;
  126. }
  127. }
  128.  
  129. //roll.displayMessage();
  130. roll.chatter();
  131. if(Wagering::wageringTest(wager, bankBalance))
  132. {
  133. games--;
  134. }
  135. else
  136. {
  137. games = 0; // to terminate the loop and exit
  138. cout << "Game over Wager or balance issue Game terminated.";
  139. }
  140.  
  141. } while ( games > 0 );
  142.  
  143. return true;
  144. }
  145. bool Wagering::wageringTest(double currentWager, double currentBankBalance)
  146. {
  147. if (currentBankBalance >= 0)
  148. {
  149. if(currentWager <= currentBankBalance)
  150. {
  151. return true;
  152. }
  153. else
  154. {
  155. return false;
  156. }
  157. }
  158. else
  159. {
  160. return false;
  161. }
  162. }
Last edited by QuantNeeds; Jun 30th, 2008 at 3:37 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 462
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 71
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: error C2677: binary '==' : no global operator found

 
0
  #2
Jun 30th, 2008
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.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,968
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: error C2677: binary '==' : no global operator found

 
0
  #3
Jun 30th, 2008
Change line 141 from :
if( gameStatus == WON )
to:
if( roll.gameStatus == WON )
And your problem should be solved
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 462
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 71
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: error C2677: binary '==' : no global operator found

 
0
  #4
Jun 30th, 2008
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
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 96
Reputation: QuantNeeds is an unknown quantity at this point 
Solved Threads: 0
QuantNeeds QuantNeeds is offline Offline
Junior Poster in Training

Re: error C2677: binary '==' : no global operator found

 
0
  #5
Jun 30th, 2008
Originally Posted by niek_e View Post
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:

  1. // Exercise 6.54 Solution: Craps.cpp - Craps simulation with wagering and chatter
  2. // Member-function definitions for Craps
  3.  
  4. // allows program to perform input and output; the program uses the following
  5. #include <iostream>
  6. using std::cout;
  7. using std::cin;
  8. using std::endl;
  9.  
  10. #include <cstdlib> // contains prototypes for functions srand and rand
  11. using std::rand;
  12. using std::srand;
  13.  
  14. #include <ctime> // contains prototype for function time
  15. using std::time;
  16.  
  17. #include "Craps.h" // include definition of class Craps
  18.  
  19. Dice::Dice()
  20. {
  21. // randomize random number generator using current time
  22. srand ( (unsigned)time ( 0 ) ); // initialize the random number generator
  23. }
  24. // function that rolls the dice and calculates the sum
  25. int Dice::rollDice()
  26. {
  27. //Dice();
  28.  
  29. int die1 = 1 + rand() % 6; // first die roll
  30. int die2 = 1 + rand() % 6; // second die roll
  31.  
  32. int sum = die1 + die2; // compute sum of die values
  33.  
  34. // display results of this roll
  35. cout << "Player rolled " << die1 << " + " << die2 << " = " << sum << endl;
  36. return sum; // end function rollDice
  37. } // end function rollDice
  38.  
  39. // function play will control the details of the game
  40. void DiceGame::play()
  41. {
  42. Dice rollDice;
  43. int sumOfDice = rollDice.rollDice(); // first roll of the dice
  44.  
  45. // determine game status and point (if needed) based on first roll
  46. switch ( sumOfDice )
  47. {
  48. case 7: // win with 7 on first roll
  49. case 11: // win with 11 on first roll
  50. gameStatus = WON;
  51. break;
  52. case 2: // lose with 2 on first roll
  53. case 3: // lose with 3 on first roll
  54. case 12: // lose with 12 on first roll
  55. gameStatus = LOST;
  56. break;
  57. default: // did not win or lose, so remember point
  58. gameStatus = CONTINUE; // game is not over
  59. myPoint = sumOfDice; // remember the point
  60. cout << "Point is " << myPoint << endl;
  61. break; // optional at end of switch
  62. } // end switch
  63.  
  64. // while game is not complete
  65. while ( gameStatus == CONTINUE ) // not WON or LOST
  66. {
  67. chatter();
  68. sumOfDice = rollDice.rollDice(); // roll dice again
  69.  
  70. // determine game status
  71. if (sumOfDice == myPoint) // win by making point
  72. gameStatus = WON;
  73. else
  74. if (sumOfDice == 7) // lose by rolling 7 before point
  75. gameStatus = LOST;
  76. } // end while
  77. }
  78. // chatter displays messages at random to create "chatter"
  79. void DiceGame::chatter()
  80. {
  81. // choose message at random
  82. switch ( rand() % 9 )
  83. {
  84. case 0:
  85. cout << "Oh, you're going for broke, huh?";
  86. break;
  87. case 1:
  88. cout << "Aw cmon, take a chance!";
  89. break;
  90. case 2:
  91. cout << "Hey, I think this guy is going to break the bank!!";
  92. break;
  93. case 3:
  94. cout << "You're up big. Now's the time to cash in your chips!";
  95. break;
  96. case 4:
  97. cout << "Way too lucky! Those dice have to be loaded!";
  98. break;
  99. case 5:
  100. cout << "Bet it all! Bet it all!";
  101. break;
  102. case 6:
  103. cout << "Can I borrow a chip?";
  104. break;
  105. case 7:
  106. cout << "Let's try our luck at another table.";
  107. break;
  108. case 8:
  109. cout << "You're a cheat! It is just a matter of time before I catch you!!!";
  110. break;
  111. } // end switch
  112.  
  113. cout << endl;
  114. } // end function chatter
  115.  
  116. bool DiceGame::test()
  117. {
  118. double wager = 100.00; // wager for current game
  119. double bankBalance = 1000.00; // current bank balance
  120. int games = 20;
  121.  
  122. cout << "\nYou have $" << bankBalance << " in the bank.\n";
  123.  
  124. do
  125. {
  126. DiceGame roll; // create object
  127. roll.play();
  128.  
  129. // display won or lost message
  130. if( roll.gameStatus == WON )
  131. {
  132. bankBalance += wager;
  133. cout << "\nPlayer wins. Your new balance is $" << bankBalance << endl;
  134. }
  135. else
  136. {
  137. bankBalance -= wager;
  138.  
  139. if(bankBalance <= 0)
  140. {
  141. cout << "\nSorry. You busted!\n";
  142. }
  143. else
  144. {
  145. cout << "\nPlayer loses. Your new balance is $" << bankBalance << endl;
  146. }
  147. }
  148.  
  149. //roll.displayMessage();
  150. roll.chatter();
  151. if(Wagering::wageringTest(wager, bankBalance))
  152. {
  153. games--;
  154. }
  155. else
  156. {
  157. games = 0; // to terminate the loop and exit
  158. cout << "Game over Wager or balance issue Game terminated.";
  159. }
  160.  
  161. } while ( games > 0 );
  162.  
  163. return true;
  164. }
  165. bool Wagering::wageringTest(double currentWager, double currentBankBalance)
  166. {
  167. if (currentBankBalance >= 0)
  168. {
  169. if(currentWager <= currentBankBalance)
  170. {
  171. return true;
  172. }
  173. else
  174. {
  175. return false;
  176. }
  177. }
  178. else
  179. {
  180. return false;
  181. }
  182. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,968
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: error C2677: binary '==' : no global operator found

 
1
  #6
Jun 30th, 2008
This: srand ( (unsigned)time ( 0 ) ); should be in main() as on of the first lines, not in the constructor.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 96
Reputation: QuantNeeds is an unknown quantity at this point 
Solved Threads: 0
QuantNeeds QuantNeeds is offline Offline
Junior Poster in Training

Re: error C2677: binary '==' : no global operator found

 
0
  #7
Jun 30th, 2008
Originally Posted by niek_e View Post
This: srand ( (unsigned)time ( 0 ) ); should be in main() as on of the first lines, not in the constructor.

thank you
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC