Stack Pop()函数出现未处理的异常错误
Unhandled Exception error with Stack Pop() function
我正在做一项涉及链表的家庭作业,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循环的主体;CCD_ 2。我真的不知道该怎么解决这个问题。如有任何建议,我们将不胜感激。
此外,如果有帮助的话,下面是我程序的大部分剩余部分:
标题:
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++;
}
Main:
#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;
}
这是一个循环链表吗?如果没有,为什么要在循环中测试:
while ( ptrNode->m_next != m_top )
ans not:
while ( ptrNode->m_next != NULL )
如果是,为什么要设置:
ptrNode->m_next = NULL;
而不是"原始"m_top->下一个??
您只需要确保循环中的第一次
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;
}
相关文章:
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- VisualC++ 2010 有没有办法找出有关未处理异常错误的更多详细信息
- C++ 程序中双链表的未处理异常
- 运算符重载时未处理<<异常
- 实验室中0x6BE20E11 (ucrtbased.dll) 的未处理异常.exe: 0xC0000005:访问冲突读取
- ImageMagick 的未处理异常
- 类数组大小为的未处理异常
- 尝试将构造函数设置为 0 - c++ 的未处理异常
- Dijkstra 中0x001e1078 s_Algorithm.exe的未处理异常:0xC0000005:访问冲突读取位置0xbaadf00d
- 运算符重载未处理异常
- 尝试使用 fread 读取文本文件时发生 'System.AccessViolationException' 类型的未处理异常
- c++中的Guard,在main中未处理异常时不调用析构函数
- 某些内存位置的未处理异常
- 错误 linkedlist .exe 中0x00D0345E处的未处理异常: 0xC0000005:访问违规写入位置0x
- 仅在发布模式下使用 glGenBuffer 的未处理异常 - QT
- 如何分析VS2010在调试模式下捕获的未处理异常
- glDrawArrays上的openGL未处理异常(GL_TRIANGLES,0,model.indicesCount)
- 使用Boost Spirit解析语法的未处理异常
- 解析"%Z" C++ 中将错误作为访问 voilation 的未处理异常给出?
- 为什么在win32计时器回调中引发的未处理异常未被调试器视为未处理异常