坚持超载操作员程序

时间:2017-04-28 01:55:30

标签: c++ operator-overloading

我遇到的问题是我的程序崩溃了,我不确定原因是什么。我试图将上述内容仅限于相关信息。所以基本上我想要发生的是当我在main中使用main.cpp中的代码时,我希望它引用我在deck.cpp部分中给出的代码。它应该打印出整个套牌,但所有发生的事情都是我的程序在打印之前崩溃了。我需要解决哪些明显的问题?非常感谢任何帮助。

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

    friend ostream& operator<<(ostream& ,const 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)
{
    Deck::card *ptr; // parsing pointer
    ptr = crd.top;
    output << "Total number of cards: " << crd.size << endl;    // Displays the number of cards in the deck
    int z = 1;
    while(ptr !=NULL)
    {
        output << z << ".  " << ptr->face << "-" << ptr->suit;                                      // Outputs the number in the deck, face, and suit of a card
        if(z%4 == 0)                                            // Creative output formatting
            output<< endl;
        else if(z < 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
        z++;                                                    // Increment the number
    }
    output << endl << endl;
    return output;
}

的main.cpp

 int main()
{

    Deck deck; // Creates a Deck Object

    deck.shuffle(1); // Shuffles one traditional deck of cards into the Deck Object

    deck.displayDeck(); // Displays the Deck of cards

    cout << "Card 1: "<<deck.getCard() << endl; // Removes top deck of the card and displays it to the screen
    cout << "Card 2: "<<deck.getCard() << endl; // Removes top deck of the card and displays it to the screen
    cout << "Card 3: "<<deck.getCard() << endl; // Removes top deck of the card and displays it to the screen

    cout << endl<<endl;

    cout << "Remove 2 Cards."<<endl;
    --deck; //  Removes a Card - Prefix
    deck--; //  Removes a Card - Postfix
    cout << endl<<endl; 

    // Alternate way of displaying the deck of cards
    cout << "Total number of cards: " << deck.getSize() << endl;
    for(int i = 1; i <=deck.getSize(); i++) // getSize() returns the number of cards in the deck
    {   
        cout <<i<<setw(4)<<left<< setfill(' ')<<".";
        cout <<setw(5)<<setfill(' ')<<left << deck[i-1]<< "\t";
        if(i%4 == 0) // Formatting to make the display a little easier to read
            cout<< endl;
    }
    cout << endl<<endl<<endl;

    cout << "Add 2 Cards."<<endl;
    ++deck; //  Adds a card back to the deck - Prefix
    deck++; //  Adds a card back to the deck - Postfix 
    cout <<endl<<endl;

    cout << deck;   // Displays the Deck of cards

    return 0;
}

0 个答案:

没有答案