C++ 被重载运算符程序卡住了

C++ 被重载运算符程序卡住了,c++,operator-overloading,C++,Operator Overloading,我遇到了一个问题,我的程序刚刚崩溃,我不确定原因是什么。我试图把上面的内容删减到只包含相关信息。所以基本上我想做的是,当我在main.cpp中使用代码时,我希望它引用我在deck.cpp部分给出的代码。它应该打印出整副牌,但所发生的一切是我的程序在打印任何东西之前崩溃。有什么尖锐的问题需要我解决吗?任何帮助都将不胜感激 甲板 class Deck { private: // Private Data Members struct card{ string fac

我遇到了一个问题,我的程序刚刚崩溃,我不确定原因是什么。我试图把上面的内容删减到只包含相关信息。所以基本上我想做的是,当我在main.cpp中使用代码时,我希望它引用我在deck.cpp部分给出的代码。它应该打印出整副牌,但所发生的一切是我的程序在打印任何东西之前崩溃。有什么尖锐的问题需要我解决吗?任何帮助都将不胜感激

甲板

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);
类甲板
{
私人:
//私有数据成员
结构卡{
弦面;//A,2,3,4,5,6,7,8,9,10,J,Q,K
弦套装;//H,C,D,S
int sn;//序列号
卡片*下一张;
};
卡片*top;//卡片组的顶部
int size;//甲板的大小
int numDecks;//此牌组中的传统牌组数

friend ostream&Operator如果它在打印“卡片总数[…]”之前已经崩溃,那么我能看到的唯一原因是,
crd
是调用
Operator时的一个悬空引用。Ori编辑了原始帖子以显示main.cpp的其余部分,而您甚至没有得到“卡片总数”输出?你有堆栈跟踪吗?如果没有,那么最好用调试器一步一步地检查它,找到它崩溃的确切位置。不,它甚至没有打印出来,我使用带有GCC的cmd行进行编译,所以我没有调试器,即使你在COM行上编译,调试器
gdb
应该仍然可用。但它可能会使用附加的
-fsanize=address
标志编译并再次运行就足够了。如果它在打印“卡片总数[…]之前已经崩溃”那么我能看到的唯一原因是,
crd
是调用
operator时的一个悬空参考。我编辑了原始帖子以显示main.cpp的其余部分,而您实际上甚至没有得到“卡片总数”输出?你有堆栈跟踪吗?如果没有,那么最好用调试器一步一步地检查它,找到它崩溃的确切位置。不,它甚至没有打印出来,我使用带有GCC的cmd行进行编译,所以我没有调试器,即使你在COM行上编译,调试器
gdb
应该仍然可用。但它可能会只需使用附加的
-fsanize=address
标志编译并再次运行即可。
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;
}
 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;
}