【问题标题】:Stack gets corrupted and won't return堆栈被损坏并且不会返回
【发布时间】:2014-03-06 23:04:24
【问题描述】:

我创建了自己的堆栈和一个重载函数。但是,当我调用该函数时,返回堆栈已损坏,我无法弄清楚原因:/我是 C++ 新手,很想学习!这是我的代码

主要

int main(){
string line;
SStack s1(1000);
SStack s2(1000);
int cap = s1.getCapacity();
cout << "Is the stack empty? " << s1.IsEmpty() << "\n";
cout << "The capacity of the stack is: " << cap << "\n";
ifstream myfile("all.last.txt");
cout << "s1 begin pushing: \n";
for (int i = 0; i <= 500; i++){
    getline(myfile, line);
    cout << "Pushing " << line << "\n";
    s1.push(line);
}
cout << "s2 begin pushing: \n";
for (int i = 0; i <= 50; i++){
    getline(myfile, line);
    cout << "Pushing " << line << "\n";
    s2.push(line);
}
myfile.close();
cout << "Is the stack empty? " << s1.IsEmpty() << "\n";
string top = s1.top();
cout << "The top object on the stack is: " << top << "\n";
cout << "The size of the stack is: " << s1.size() << "\n";
cout << "Popping: " << s1.pop() << "\n";
cout << "Size after pop is: " << s1.size() << "\n";
s1 = s1 + s2;
cout << s1.top();

}

不返回的SStack函数

    SStack::SStack(const SStack& s) : used(-1), Capacity(0), DynamicStack(0){
    *this = s;
}

SStack SStack::operator=(const SStack& s){
    if (this != &s){
        int cap = s.getCapacity();
        DynamicStack = new string[cap];
        Capacity = cap;
        used = -1;
        for (int count = 0; count < s.size(); count++){
            DynamicStack[count] = s.DynamicStack[count];
            used++;
        }
        }
        return *this;
}

SStack SStack::operator +(const SStack& s2){
int size1 = used + 1;
int size2 = s2.size();
SStack result = *this;
if (size1 + size2 <= Capacity){
    for (int count = 0; count < s2.size(); count++){
        result.push(s2.DynamicStack[count]);
    }
    return result;
}
else{
    cout << "Error stack is not big enough";
    return result;
}

【问题讨论】:

  • 请将此减少到演示问题所需的最低程序。
  • 使用调试器,设置断点,观察变量。不要只是转储大量代码。请参阅this site 了解如何就 SO 提出好的问题。
  • ... 并包含所有需要的内容,例如缺少类定义(“SStack.h”)。
  • 我已经使用了断点,但仍然无法找出问题所在。堆栈添加正确,但当它返回时调用析构函数并删除所有信息
  • 哪个函数没有返回?

标签: c++ stack overloading


【解决方案1】:

您在 2 个位置分配堆栈实例,但我没有看到 operator= 定义。 我建议你在你的 SStack 类中添加一个 operator=() 并将你的复制构造函数委托给这个 operator=()。确保您的 operator=() 检查它是否在复制自己:

...
SStack::SStack(const SStack &s) : used(-1), Capacity(0), DynamicStack(0)
{ 
    *this = s; 
}
SStack &SStack::operator=(const SStack &s)
{
    if (this != &s && s.size() > 0)
    {
        // check if we already have an allocated array
        if (Capacity < s.size())
        {
            // safe even if Capacity==0 as long as DynamicStack==0 too
            delete [] DynamicStack;  

            int cap = s.getCapacity();
            DynamicStack = new string[cap];
            Capacity = cap;
        }
        used = -1;
        for (int count = 0; count < s.size(); count++){
            DynamicStack[count] = s.DynamicStack[count];
            used++;
        }
    }
    return *this;
}

用调试器试试这个,我建议你重新检查你的“使用”变量,以确保它在所有使用它的地方都有你想要的索引。

希望这会有所帮助。

【讨论】:

  • 复制操作和初始化一样?
  • 我添加了一个编辑来澄清,你在那里初始化你的变量,这样当你输入你的 operator=() 函数时它们的值就不会未定义。复制操作与您在当前复制构造函数中所做的完全相同,只是多了一个转折点。 operator=() 应该检查当前分配、容量等,并释放所有内容,以便在当前空间不足时分配足够的空间来复制整个外部堆栈。
  • 好的,我现在试试
  • 关于可读性的另一个建议:通常,数据成员在代码中以 _ 为前缀或后缀来标识它们。例如。对于这个数据成员,我会使用 m_used、_used 或 used_。
  • 哦,好吧。我似乎遇到了堆栈溢出错误。当操作员调用它时,它会不断生成新的堆栈值。我更新了上面的代码
猜你喜欢
  • 2016-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-13
  • 2014-01-07
相关资源
最近更新 更多