C ++->断言失败堆栈

科迪

我正在为我的CS类获取一堆字符串(我自己创建的String ADT),但是我被其中的一部分所困,无法弄清楚出了什么问题。当我使用Makefile编译代码时,出现错误:test_default_ctor:./stack.hpp:53:T stack :: pop()[T = String]:断言“ TOS!= 0”失败。

由于我在头文件中声明了pop()函数而导致测试未运行的地方。这是我的代码片段:

#ifndef STACK_HPP
#define STACK_HPP

#include <iostream>
#include <cassert>
#include <new>

#include "string/string.hpp"

template <typename T>
class node{
public:
    node(): next(0), data(){};
    node(const T& x): next(0), data(x){};

    node<T> *next;
    T data;
    // EXAM QUESTION: This class is going to be used by another class so all needs to be accessable
    // Including node p and node v.
};

template <typename T>
class stack{
public:
    stack(): TOS(0){};
    ~stack();
    stack(const stack<T>&);
    void swap(stack<T>&);
    stack<T>& operator=(stack<T> rhs){swap(rhs); return *this;};
    bool operator==(const stack<T>&) const;
    bool operator!=(const stack<T>& rhs) const {return !(*this == rhs);};
    friend std::ostream& operator<<(std::ostream&, const stack<T>&);
    bool isEmpty()const{return TOS == 0;};
    bool isFull(void)const;
    T pop(void);
    void push(const T&);
    int slength()const{String nuevo; return nuevo.length();};
private:
    node<T> *TOS;
};

template <typename T>
T stack<T>::pop(){
    assert(TOS!=0);
    node<T> *temp=TOS;
    T result=TOS -> data;
    TOS=TOS -> next;
    int len=slength();
    --len;
    delete temp;
    return result;
}

template <typename T>
bool stack<T>::operator==(const stack<T>& rhs) const{
    stack<T> left = *this;
    stack<T> right = rhs;

    if(slength() != rhs.slength())
            return false;   
    if(left.pop() != right.pop())
            return false;     
    else                 
            return true;
}

这是我的测试:

#include "stack.hpp"
#include "string/string.hpp"

#include <cassert>

int main()
{
    {
            stack<String> test;
            assert(test == stack<String>());
            assert(test.slength()==0);
    }


    std::cout<<"Done testing default constructor!"<<std::endl;

    return 0;
}

我知道这种情况的发生是因为栈顶(TOS)为0,但是我不知道为什么即使在测试中根本没有调用pop函数,断言也不会通过它。ANyone能够提供任何帮助吗?

匿名邮件

发布operator==代码后,很明显发生了什么。该操作员正在调用pop一个空堆栈。您必须重写operator==函数,以使其不使用poppop修改您不想使用的堆栈。您应该遍历堆栈的每个节点,并比较它们的相等性。

另外,我将消除我在下面突出显示的两行内容。那就是在复制堆栈,通常情况下,如果不需要的话,您就不想这样做。另外,由于您的类包含指针,因此您应该编写operator=正确的指针由于代码使用的是编译器生成的默认值,operator=因此它是不正确的。

template <typename T>
bool stack<T>::operator==(const stack<T>& rhs) const{
    stack<T> left = *this;  // remove this
    stack<T> right = rhs;   // remove this

    if(slength() != rhs.slength())
            return false;   
    if(left.pop() != right.pop())
            return false;     
    else                 
            return true;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C ++和OpenCV断言失败

malloc.c:3096:SYSMALLOC:断言失败

OpenCV C ++断言失败调用drawContours

调试断言失败。C ++向量下标超出范围

初始化常量变量,如果失败则断言(C ++)

调试断言失败。C ++向量下标超出范围

使用 Opencv 时 C++ 调试断言失败

我gettein检测到错误C ++断言失败堆损坏

C ++调试断言失败。向量下标超出范围

SHA-1 C ++中的堆栈检查失败

断言断言失败

调试断言失败,表达式:(unsigned)(c +1)<= 256提问c ++

使用malloc的C语言中的错误:malloc.c:2451:sYSMALLOc“断言失败”

特征错误:用特征替换 fftw_malloc 数组后静态断言失败 C++

在QML中为C ++类型的QObject *属性分配null时断言失败

C ++ 11静态断言是否会失败,除非使用Clang ++进行检查?

Z3的C ++ API求解器检查中的Z3_MEMOUT_FAIL断言失败?

堆损坏 - 调试断言失败。在 dbgheap.c 第 1322 行表达式 _crtIsValidHeapPointer(pUserData)

Qt C ++:静态断言失败:信号和插槽参数不兼容

断言失败错误,C++中的向量下标超出范围问题

C ++调试断言失败;表达式:列表迭代器不兼容

cvt::Color 中的 C++ OpenCV 异常断言失败 (scn ==3|| scn == 4)

如何在 C++ 中修复 SDL2“SDL_RenderClear_REAL 处的断言失败”?

有什么办法可以避免 C++ 中的“调试断言失败”窗口?

Calabash-iOS:每次断言失败时都会遇到堆栈级别太深(SystemStackError)错误

错误消息:“在脉冲/mainloop.c上断言't = find_next_time_event(m)'失败”吗?...(C ++)

警告 C6255:_alloca 通过引发堆栈溢出异常指示失败

C ++-运行时检查失败#2-围绕变量'sourceCount'的堆栈已损坏

C-“运行时检查失败#2-围绕变量'str'的堆栈已损坏。”