c++ - 试图重载 << 运算符,但出现错误

标签 c++ operator-overloading

甲板.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);

甲板.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 实现的范围内声明,但我不知道如何使用友元定义。非常感谢对此的任何帮助。

最佳答案

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

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

Deck::card *ptr;

并指定你想要接收成员的对象:

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

关于c++ - 试图重载 << 运算符,但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43669253/

相关文章:

c++ - 将字符串转换为int的简单方法? C++

c++ - 如果包为空,是否对可变参数包类型执行替换?

operator-overloading - Rust 看不到我重载的 f64 乘法运算符

c++ - + 运算符重载以添加不同的 C++ 类对象

c++ - 运算符重载内存分配?

c# - 实现特定运算符的对象的 .NET 接口(interface)/约束

用于连接的 Python 运算符重载

C++ getline cin错误

c++ - 在 cpp 中重载 << 运算符时出错?不知道为什么?

c++ - boost::spirit::qi 对输出进行重复解析