Bestweavena
Bestweavena

Reputation: 13

Trying to Overload << operator, but getting error

Deck.h

class Deck
{
    private:

        // Private Data Members
        struct card{
            string face; // A,2,3,4,5,6,7,8,9,10,J,Q,K
            string suit; // H,C,D,S
            int sn;      // Serial Number
            card* next;
        };

        card *top;       // Top of the deck
        int size;        // Size of the deck
        int numDecks;    // Number of traditional decks in this deck of cards   

        // Utility Functions

        string calcF(int);  //Calculates face from random number associated with card;
        string calcS(int);  //Calculates suit from random number associated with card;

        bool isDuplicate(int);
        void pop();  //POP
        void push(); //PUSH

    public:
        Deck(); //  Constructor
        ~Deck();//  Deconstructor


        //Mutators
        string getCard();   // Returns FF-S (FF-Face, S-Suit) of the top card and deletes the top card from the deck
        void shuffle(int);  //Adds nodes over and over again until the size of the Deck reaches the number of decks passed times 52

        //Accessors
        void displayDeck(); // Displays the total number of cards and the cards in the deck
        string getFace();   // Returns the face of the top card
        int getSize();      // Returns the number of cards that are currently in the deck
        int getSN();        // Returns the sn   of the top card
        string getSuit();   // Returns the suit of the top card

        friend ostream& operator<<(ostream& , Deck& );
        string operator[](int );
        Deck operator--();       // Prefix decrement operator.  
        Deck operator--(int);
        Deck operator++();       // Prefix decrement operator.  
        Deck operator++(int);

Deck.cpp

ostream& operator<<(ostream& output, const Deck& crd)
{
    card *ptr; // parsing pointer
    ptr = top;
    output << "Total number of cards: " << size << endl;    // Displays the number of cards in the deck
    int i = 1;
    while(ptr !=NULL)
    {
        output << i << ".  " << ptr->face << "-" << ptr->suit;                                      // Outputs the number in the deck, face, and suit of a card
        if(i%4 == 0)                                            // Creative output formatting
            output<< endl;
        else if(i < 10)                                         // More creative output formatting
            output <<"\t\t";
        else                                                    // Even more creative output formatting
            output << "\t";
        ptr = ptr->next;                                        // Move the parsing pointer to the next card
        i++;                                                    // Increment the number
    }
    output << endl << endl;
    return output;
}

The errors I'm getting is that ptr, top, and size arent declared in the scope of the cpp implementation, but I don't see how as I'm using a friend definition. Any help on this would be very much appreciated.

Upvotes: 0

Views: 67

Answers (1)

aschepler
aschepler

Reputation: 72473

A friend is not a member of the class, nor in the scope of the class when defined outside the class definition.

You need to specify the card type with its full name:

Deck::card *ptr;

and specify what object you want to take members of:

ptr = crd.top;
output << "Total number of cards: " << crd.size << endl;

Upvotes: 2

Related Questions