question regarding classes

Hey everyone, I am having trouble with a program dealing with Poker hands. I used 2 separate Classes, Card and friend class Hand(containing 5 cards), with the aim of setting 5 card hand using the input from the user. I wrote the set() functions for Card no problem. However I cannot get the coding for set() for class Hand right no matter what I tried.

Anyway this is the relevant code I have done so far,

.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Card
{
  public:
    friend class Hand; 
    void set(int newSuit, int newFace);
  private:
    int suit; 
    int face;  
};

class Hand
{
  public:  
  void set(Card newCard1, Card newCard2, Card newCard3, Card newCard4, Card newCard5)
  private:
    Card card1;  
    Card card2;   
    Card card3;           
    Card card4;   
    Card card5; 
};


.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Card::set(int newSuit, int newFace)
{
  suit = newSuit;
  face = newFace;
}

void Hand::set(Card newCard1, Card newCard2, Card newCard3, Card newCard4, Card newCard5)
{
 
  newCard1.set(newCard1.suit, newCard1.face);
  newCard2.set(newCard2.suit, newCard2.face);
  newCard3.set(newCard3.suit, newCard3.face);
  newCard4.set(newCard4.suit, newCard4.face);
  newCard5.set(newCard5.suit, newCard5.face);
}


.main
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  int suit1, suit2, suit3, suit4, suit5;
  int face1, face2, face3, face4, face5;  

  cout << "Enter a hand (10 integers): " << flush;
  cin >> suit1 >> face1 >> suit2 >> face2 >> suit3 >> face3 >> suit4 >> face4 >> suit5 >> face5;
  
  Card newCard1, newCard2, newCard3, newCard4, newCard5;
  Hand newHand;
  
  newHand.set(newCard1, newCard2, newCard3, newCard4, newCard5);


Does the error come from the implementation of the void Hand::set function? Or is there something wrong with the way I am calling it in the main? Any hints or help would be greatly appreiciated!!
Your Hand::Set function doesn't do anything at all...it's basically setting values to themselves.
Thanks, for that,

The correction I tried to implement was
1
2
3
4
5
6
7
8
9
void Hand::set(Card newCard1, Card newCard2, Card newCard3, Card newCard4, Card newCard5)
{
 
  card1.set(newCard1.suit, newCard1.face);
  card2.set(newCard2.suit, newCard2.face);
  card3.set(newCard3.suit, newCard3.face);
  card4.set(newCard4.suit, newCard4.face);
  card5.set(newCard5.suit, newCard5.face);
}


That doesn't seem to work either.
JamesZ wrote:
Does the error come from

It sure would help if you explained what "the error" is supposed to be. We can't read minds.

You should also read the following:
http://www.cplusplus.com/forum/beginner/1/

A perhaps more appropriate design of the Card class is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Card
{
  public:
    Card(int suit,int face) : suit(suit), face(face) {}
    const Card& operator=(const Card& rhs)
    {
      suit=rhs.suit;
      face=rhs.face;
      return *this;
    }

    int getSuit() const {return suit;}
    int getFace() const {return face;}

  private:
    int suit; //should be an enum
    int face; //same here
};


set is essentially what operator= is designed for and instead of adding random classes as friends, you should provide getters which all current and future classes can use.
Last edited on
Maybe calling newCard1.set(suit1,face1); ... etc before newHand.set(...); would help ;)

EDIT:

Athar wrote:
instead of adding random classes as friends, you should provide getters

+1

kfmfe04 (below) wrote:
+1 m4ster r0shi

+1
Last edited on
+1 m4ster r0shi

In your main file, between Line 9 and Line 12, you've instantiated newCard1, newCard2, newCard3, newCard4, newCard5, but they haven't been initialized to anything. Without a default constructor for Card(), the contents of those instances are UNDEFINED (they can contain anything).
Nice, thanks m4ster r0shi, I knew I missed something but just couldn't figure out what.

Also thanks Athar for the advice, just that the assessment was set out with a header file that could not be modified, so the format may not be the most efficient, but nevertheless one I have to follow by.
Topic archived. No new replies allowed.