C++ -> 断言失败堆栈
C++ -> Assertion failure Stack
我正在为我的CS类获取一堆字符串(我自己制作的字符串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函数。有人能提供帮助吗?
发布operator==
代码后,很明显发生了什么。该运算符正在空堆栈上调用pop
。您必须重写operator==
函数,这样它就不会使用pop
。如果你不想使用pop
,它会修改堆栈。你应该循环遍历堆栈的每个节点,并比较它们是否相等。
此外,我将删除下面我强调的两行。这是对堆栈进行复制,通常情况下,如果不需要,您不想这样做。此外,由于您的类包含指针,因此应该正确地编写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;
}
相关文章:
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- uint_not_usable_without_attribute在业力规则中使用数字生成器时静态断言失败
- 从 exe 文件 (Visual Studio ) 启动时调试断言失败
- 在 CppUnit 中测试中止断言失败
- 使用扫描的调试断言失败
- 为什么我的Qt程序在断言失败后继续运行?
- 图片不显示,关闭时出错 --> 调试断言失败!表达式:is_block_type_valid(标头>_block_use)
- 访问提升:shared_ptr 主范围外崩溃,断言失败:px != 0.指针的正确用法是什么?
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 错误:断言失败 (src.type() == CV_8UC1) 在阈值中
- Boost Beast 异步服务器失败,断言失败:(id_ != T::id) 在多个 aync 调用中
- 使用imwrite OpenCV时断言失败
- 调试断言失败的缓冲区!=nullptr
- 使用 ofstream 写入文本文件时断言失败
- 提升继续恢复断言失败
- 获取有关调试断言失败的错误:表达式:"(_Ptr_user &(_BIG_ALLOCATION_ALIGNMENT -1)) == 0" &&0
- OpenCV(3.4.1) 错误:断言失败(变暗 <= 2 && step[0] > 0) in cv::Mat::locateROI
- 在Win10上使用带有Qt5的OpenCV3.0,调试断言失败
- MFC GetDC() 断言失败 - "Not a window"
- OpenCV 错误:断言失败 - 矩阵减去