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,
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!!
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.
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.