打印字符串时出错,即使它在函数中工作正常 一个简单的基于C++指针的堆栈程序。我试图打印一个字符串,它是PointerStack类用作其项类型的NameItem类的一部分(请参见代码)。每当我试图在程序的main()函数中打印字符串时,控制台就会反复打印乱码和嘟嘟声。但是,当我调用PrintPointerStack函数时,没有错误,所有内容都按预期打印

打印字符串时出错,即使它在函数中工作正常 一个简单的基于C++指针的堆栈程序。我试图打印一个字符串,它是PointerStack类用作其项类型的NameItem类的一部分(请参见代码)。每当我试图在程序的main()函数中打印字符串时,控制台就会反复打印乱码和嘟嘟声。但是,当我调用PrintPointerStack函数时,没有错误,所有内容都按预期打印,c++,windows,C++,Windows,我尝试过改变类,重新排列代码,虽然我可以确定哪一行产生了错误,但我不知道为什么。我在这里完全迷路了,以前从未见过这样的事情,所以如果答案很简单,在谷歌搜索中找到,我很抱歉,但我已经走了好几个小时,只是不知道该搜索什么了 代码如下: #include <iostream> #include <string> #include <stack> #include <cstddef> #include <new> using namespace

我尝试过改变类,重新排列代码,虽然我可以确定哪一行产生了错误,但我不知道为什么。我在这里完全迷路了,以前从未见过这样的事情,所以如果答案很简单,在谷歌搜索中找到,我很抱歉,但我已经走了好几个小时,只是不知道该搜索什么了

代码如下:

#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;
}
和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 << " ";
}
#包括
#包括“NameItem.h”
NameItem::NameItem()
{
name=“”;
}
RelationType NameItem::ComparedTo(NameItem otherItem)常量
{
if(nameotherItem.name)
回报更大;
其他的
回报相等;
}
void NameItem::Init(字符串值)
{
名称=值;
}
void NameItem::Print()常量
{

这个问题是双重的

首先,您正在清空
PrintPointerStack()
中的
PStacker
对象,然后尝试访问该空堆栈的顶部元素。这将抛出一个
EmptyStack
。如果没有发生此情况,则表明还有另一个问题(请参见下文)

第二,giberish被打印(有时)的事实表明您试图通过无效的对象/指针访问数据
通过传递值,调用默认的复制构造函数,盲目复制
top
指针的值。然后继续删除对象,但原始
PStacker
中的
top
指针没有更改,因此现在无效。因此,您的问题就出现了


要修复此问题,您需要通过指针/引用将参数传递给
PrintPointerStack()
,或者提供一个更合适的复制构造函数来执行深度复制(而不是默认复制构造函数提供的浅层复制)。

\include“NameItem.cpp”
-您应该在声明中包含标题(接口)而不是.cpp文件(实现)。的确,这会很有用,但从OP提供的代码中重建头相对容易。啊,我还没有使用复制构造函数的经验,所以这就是问题所在。我实现了一个,我的问题得到了解决,感谢你为我指明了正确的方向。
#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 << " ";
}