BlackJack problem

Hiya I am just trying to learn C++ on my own - I am 56 yrs old and did it yrs ago so now that I am retired I have time to work on this again.
My problem is in the function I am working on to 'Load' the cards. It is not complete but I did enough to load one card. Then when I attempt to print it to the screen I am getting errors - I think that is called overloading the << operator. I do not know how to do that yet .. maybe its not called that .. You guys will know better on that.

I am lost how to display these values to the screen.
Here is my whole program so far ...

[cpp CODE]
#include<iostream>
#include<ostream>
#include<string>
#include<cmath>

using namespace std;

/* Date : Sept 14, 2018
Name : BlackJack v1.00 Purpose: To Play BlackJack
Lesson :
This lesson I am working with
Structures, Classes, and enumerations.
Concept :

I decided early on to separate the cards class from the game
class. I am using structs for my enumerations :Suits, and Face.
I want Game Play to be dealt with of its own. Therefore, my tasks
will be as follows:
1.) Load the deck with the proper card face, suit, and value.
2.) Shuffle and Deal the cards to each player
(Shuffle will be called from within Function Deal),
Keep Track of Cards Pulled From Deck !!
3.) Ask player(s) if they want to <HIT> or <STAY>

4.) If player <STAYS> then evaluate winner.
5.) Game Over if player Bust.
6.) Game Over if Dealer beats Player.
7.) Game Over if player has Five - Card - Charlie
(automatic win).
8.) Game Over if Dealer or Player is Dealt BlackJack
But not for 21 (Dealer Takes All Ties).
--- --- --- --- --- --- --- --- --- --- --- */
enum struct Suit{Hearts=0, Clubs, Diamonds, Spades};
enum struct Face{Ace=0,Two, Three, Four, Five, Six, Seven,
Eight, Nine, Ten, Jack, Queen, King};
// ==============================
struct Card{
public:
Face face;
Suit suit;
int value;};
// =======================================
class Game{
private:
Card standard[52];
public:
void CreateDeck(Game x);
void DealCards();};
// ========================================
int main(){
Game BlackJack;
BlackJack.CreateDeck(BlackJack);
return 0;}
// ~~~~~~~~~~~~~~ Function Line ~~~~~~~~~~~~~~~~
void Game::CreateDeck(Game x){ // only partially completed
for (int i=0;i<13;i++){
x.standard[i].face=Face(i);}
/* ----------- ERROR --------
This line gives me an error it says :
No Match for operator << in std::cout cout<<x.Game::standard[4]

I was only trying to print this so I can check value.
I only completed part of the function so I can see if it
is writing it in correctly. If this worked I was going to
finish the function to include suit of card.
I can't get past this ....


cout<<x.standard[0];
*/
}

[/CODE]

 
cout<<x.standard[0];


x.standard[0] is an object of type Card; it is the first Card object in the big array of cards in the Game object named x.

So you're passing an object of type Card to the operator <<.

C++, of course, has never heard of a Card object. Doesn't know what it is. Doesn't know what to do with it. Nobody has ever written the operator:
ostream& operator<<(ostream& os, const Card& cardObject)
which is the operator you're trying to call here.

So, in order to call that operator, you have to write it yourself.

This ought to do it:
1
2
3
4
5
6
7
ostream& operator<<(ostream& os, const Card& cardObject)
{
     os << "Value: ";
  os << cardObject.value;
  os << '\n';
  return os; 
} 


Like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Example program
#include <iostream>
#include <string>

using namespace std;

enum struct Suit{Hearts=0, Clubs, Diamonds, Spades};
enum struct Face{Ace=0,Two, Three, Four, Five, Six, Seven,
Eight, Nine, Ten, Jack, Queen, King};
// ==============================
struct Card{
public:
Face face;
Suit suit;
int value;};

ostream& operator<<(ostream& os, const Card& cardObject)
{
     os << "Value: ";
  os << cardObject.value;
  os << '\n';
  return os; 
}
// =======================================
class Game{
private:
Card standard[52];
public:
void CreateDeck(Game x);
void DealCards();};
// ========================================
int main(){
Game BlackJack;
BlackJack.CreateDeck(BlackJack);
return 0;}
// ~~~~~~~~~~~~~~ Function Line ~~~~~~~~~~~~~~~~
void Game::CreateDeck(Game x){ // only partially completed
for (int i=0;i<13;i++){
x.standard[i].face=Face(i);}

cout<<x.standard[0];

}


In this new code, the operator you're trying to call IS now defined. You can see that all it does at the moment is call << again on the stream, with the int Value. C++ does already know what to do with an int being used like that (i.e. somewhere, someone else already wrote the operator ostream& operator<<(ostream& os, const int& someIntValue) for you.

I just output one of the Card object's variables. If you want Face and Suit as well you'll need to write that code. Of course, there is no operator ostream& operator<<(ostream& os, const Face& someFaceValue) ; you will have to think about what code to write. Could be something like this:

1
2
if (suit == Hearts) { os << "Hearts";}
etc.


You will note that this operator is making use of your struct's public variable. If your struct had private variables that you wanted to use like this, you would need to tell C++ that this operator was a friend of your struct, because a friend is allowed to see the private variables.

There is an example of that here: https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx


Hey, thanks a bunch for all of that - - it worked and I am going to do just what you said.
(You're really good !!)

Thank You very Kindly
Bob
Topic archived. No new replies allowed.