【问题标题】:Unhandled Exception error with Stack Pop() functionStack Pop() 函数未处理的异常错误
【发布时间】:2013-12-04 05:33:10
【问题描述】:

我正在处理一项涉及链接列表的家庭作业,但我在使用 Pop() 函数时遇到了问题。这是我的代码:

void CardStack::Pop( )
{
    if ( m_top->m_next == NULL )
    {
        delete m_top;
        m_top = NULL;
    }

    else
    {
        Node* ptrNode = m_top;

        // gets 2nd to last node
        while ( ptrNode->m_next != m_top )
        {
            ptrNode = ptrNode->m_next;
        }
        // delete last node, then set second to last to null
        delete m_top;
        m_top = NULL;

        ptrNode->m_next = NULL;
        m_top = ptrNode;
    }
    m_size--;
}

我在运行程序时不断收到此运行时错误,在 if 语句处中断:

作业 7 中 0x008e2009 处的未处理异常 - CS201.exe:0xC0000005: 访问冲突读取位置 0x00000004。

我的一个朋友建议从程序中删除 m_top = NULL,这使得 if 语句有效。我宁愿保持空值不变,但它让程序运行起来。无论如何,它会在 while 循环中中断并出现此错误:

作业 7 中 0x00b91f4a 处的未处理异常 - CS201.exe:0xC0000005: 访问冲突读取位置 0xfeeefeee。

我用 cout 语句对其进行了测试,错误来自 while 循环体; ptrNode = ptrNode->m_next。我不确定如何解决这个问题。任何建议将不胜感激。

另外,如果有帮助,这里是我的程序的大部分其余部分:

标题:

class Node
{
public:
    friend class CardStack;

private:
    Node* m_next;
    int m_data;
};

class CardStack
{
public:
    CardStack();
    ~CardStack();

    void Push( int value );
    void Pop();
    int Top();
    int GetSize();

private:
    int m_size;
    Node* m_top;
};

构造函数、析构函数、Push():

#include <iostream>
#include "CardStack.h"
using namespace std;

CardStack::CardStack( )
{
    m_top = NULL;
    m_size = 0;
}

CardStack::~CardStack( )
{
    while ( m_top != NULL )
    {
        Pop( );
    }
}

void CardStack::Push( int value )
{
    //creates new node
    Node* newNode = new Node;
    newNode->m_data = value;
    newNode->m_next = NULL;

    // tests if the list is empty
    if ( m_top == NULL )
    {
        m_top = newNode;
    }
    // if the list does contain data members and a last node exists
    else
    {
        m_top->m_next = newNode;
        m_top = newNode;
    }
    m_size++;
}

主要:

#include <iostream>
#include <string>
#include <fstream>
#include "CardStack.h"
using namespace std;

void loadDeck ( ifstream&, CardStack& );
void playGame ( CardStack& );

void loadDeck ( ifstream& inFile, CardStack *card )
{
    int currentCard;

    while( !inFile.eof() )
    {
        inFile >> currentCard;
        card->Push( currentCard );
    }
}

void playGame( CardStack *card )
{
    int score[ 4 ];
    int player;
    while( card->GetSize() != 0 )
    {
        player = card->GetSize() % 4;
        score[ player ] += card->Top();
        card->Pop();
    }

    for ( player = 0; player < 4; player++ )
        cout << "Player " << player << "'s score is " << score[ player ] << endl;
}

int main()
{
    CardStack *card = new CardStack;
    string fileName;
    ifstream inFile;

    do
    {
        cout << "Input the name of the card file (not including extension): ";
        cin >> fileName;

        fileName += ".txt";

        inFile.open( fileName );

        if ( !inFile.is_open() )
            cout << "File could not be opened. Reenter the file name." << endl;
    }
    while( !inFile.is_open() );

    loadDeck( inFile, card );
    playGame( card );

    inFile.close();

    system( "pause" );
    return 0;
}

【问题讨论】:

    标签: c++ exception singly-linked-list unhandled


    【解决方案1】:

    这是一个循环链表吗? 如果不是,你为什么要在循环中测试:

    while ( ptrNode->m_next != m_top )
    

    不是:

    while ( ptrNode->m_next != NULL )
    

    如果是,你为什么要设置:

    ptrNode->m_next = NULL;
    

    而不是“原始” m_top->next??

    【讨论】:

    • 我一直在使用我的教授提供的示例代码来制作堆栈功能。我以为我理解了代码,但现在我很确定它是一个循环链表,我正在尝试制作一个线性链表。现在有点像白痴的感觉。所以现在我认为我的主要问题是让 ptrNode 指向列表的开头或者不是顶部。我能想到的唯一值就是 Push() 中的 newNode。
    【解决方案2】:

    你只需要确保循环中的第一次

    while ( ptrNode->m_next != m_top )
        {
            ptrNode = ptrNode->m_next;
        }
    

    ptrNode 不会为 NULL, 因为你已经在 if 块中检查了它, 但是当第二次循环时,ptrNode 可能是 NULL,`因为现在它实际上是

    m_top->m_next->m_next
    

    但是如何确定m_top->m_next不为NULL,为NULL时会崩溃

    也许你应该把它改成

    while ( ptrNode->m_next != NULL )
    {
        ptrNode = ptrNode->m_next;
    }
    

    另外,还有一个问题,在你的推送功能中

    else
    {
        m_top->m_next = newNode;
        m_top = newNode;
    }
    

    应该是

    else
    {
        newNode->m_next = m_top;
        m_top = newNode;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多