试图重载<<操作员,但得到错误

时间:2017-04-27 23:01:24

标签: c++ operator-overloading

加入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;
}

我得到的错误是ptr,top和size没有在cpp实现的范围内声明,但我不知道我如何使用朋友定义。对此有任何帮助将非常感激。

1 个答案:

答案 0 :(得分:2)

在类定义之外定义时,friend不是类的成员,也不在类的范围内。

您需要指定card类型的全名:

Deck::card *ptr;

并指定您想要成员的对象:

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