处理一个简单的基于C ++指针的堆栈程序。我试图打印一个字符串,它是NameItem类的一部分,PointerStack类将其用作项类型(请参阅代码)。每当我尝试在程序的main()函数中打印字符串时,控制台会反复打印乱码和嘟嘟声。但是,当我调用PrintPointerStack函数时,没有错误,所有内容都按预期打印。
我试过更改类,重新排列代码,虽然我可以确定哪一行产生错误但我无法弄清楚原因。我完全迷失在这里,之前从未见过这样的事情,所以如果答案很简单并且在谷歌搜索中找到,我很抱歉,但我已经去了几个小时,只是不知道该搜索什么。< / p>
代码如下:
#include <iostream>
#include <string>
#include <stack>
#include <cstddef>
#include <new>
using namespace std;
#include "NameItem.cpp"
#include "Stack.cpp"
#include "PointerStack.cpp"
void PrintPointerStack(PointerStack printer){
NameItem temp;
while(!printer.IsEmpty()){
temp = printer.Top();
printer.Pop();
temp.Print();
}
cout << endl;
}
int main(){
string initNames[] = {"Michael","Charlie","Susan","Alexa",
"Jason","Candice","Beatrice","Lois",
"Peter","Matthew"};
int initNamesLen = 10;
PointerStack PStacker, tempPStacker;
NameItem filler;
for(int i = 0; i < initNamesLen; i++){
filler.Init(initNames[i]);
PStacker.Push(filler);
}
cout << endl << "---------- Pointer-based Stack ----------" << endl << endl;
PrintPointerStack(PStacker);
cout << "Top: ";
(PStacker.Top()).Print(); //This is where the program errors. I've tried creating a
//temp variable like in the function above, and I've
//tried accessing the string directly and printing it
//from main() using cout, which produce the same results.
//So the error is caused specifically by the cout <<
//string statement, when I try to use that statement
//within the bounds of the main function.
cout << endl;
PrintPointerStack(PStacker);
cout << endl << "Popped: ";
(PStacker.Top()).Print();
PStacker.Pop();
(PStacker.Top()).Print();
PStacker.Pop();
cout << endl;
PrintPointerStack(PStacker);
cout << endl << "Pushed: Sammy Valerie" << endl;
filler.Init("Sammy");
PStacker.Push(filler);
filler.Init("Valerie");
PStacker.Push(filler);
PrintPointerStack(PStacker);
try{
PStacker.Push(filler);
}
catch(FullStack){
cout << endl << "Stack is full, name not pushed" << endl;
}
cout << endl << "Popped: ";
while(!PStacker.IsEmpty()){
filler = PStacker.Top();
PStacker.Pop();
filler.Print();
}
try{
PStacker.Pop();
}
catch(EmptyStack){
cout << endl << "Stack is empty, name not popped" << endl;
}
return 0;
}
PointerStack类
#include "PointerStack.h"
PointerStack::PointerStack(){
top = NULL;
}
/*PointerStack::~PointerStack(){
Node* temp;
while(top != NULL){
temp = top;
top = top->next;
delete temp;
}
}*/
void PointerStack::Push(NameItem item){
if(IsFull())
throw FullStack();
else{
Node* location;
location = new Node;
location->data = item;
location->next = top;
top = location;
}
}
void PointerStack::Pop(){
if(IsEmpty())
throw EmptyStack();
else{
Node* temp;
temp = top;
top = top->next;
delete temp;
}
}
NameItem PointerStack::Top(){
if(IsEmpty())
throw EmptyStack();
else{
return top->data;
}
}
bool PointerStack::IsEmpty() const{
return (top == NULL);
}
bool PointerStack::IsFull() const{
Node* location;
try{
location = new Node;
delete location;
return false;
}
catch(std::bad_alloc& exception){
return true;
}
}
NameItem类
#include <fstream>
#include "NameItem.h"
NameItem::NameItem()
{
name = " ";
}
RelationType NameItem::ComparedTo(NameItem otherItem) const
{
if (name < otherItem.name)
return LESS;
else if (name > otherItem.name)
return GREATER;
else
return EQUAL;
}
void NameItem::Init(string value)
{
name = value;
}
void NameItem::Print() const
{
cout << name << " ";
}
最后请注意,主程序有更多代码用于测试程序中包含的Stack类。我删除了代码,因为它与错误无关,程序仍然崩溃,但它会立即崩溃,出现一个Windows错误框,而不是控制台乱码/嘟嘟声。不确定这是否相关......
答案 0 :(得分:2)
问题是双重的。
首先,您要清空PStacker
中的PrintPointerStack()
对象,然后尝试访问该空堆栈的顶部元素。这应该抛出EmptyStack
。没有发生这种情况的事实也表明了另一个问题(见下文)。
其次,打印giberish(有时)的事实表明您试图通过无效的对象/指针访问数据。实际上,因为您通过pass-by-value传递PrintPointerStack()
的参数,所以调用默认的复制构造函数,盲目地复制top
指针的值。然后继续删除对象,但原始top
中的PStacker
指针未更改,因此现在无效。因此你的问题。
要修复,您需要通过指针/引用将参数传递给PrintPointerStack()
,或者提供一个更好的适合复制构造函数来执行深层复制(而不是默认复制构造函数提供的浅复制)