C++ C++-&燃气轮机;断言失败堆栈
我正在为我的CS课程准备一堆字符串(我自己制作的字符串ADT),但我被抓住了其中的一部分,无法找出哪里出了问题。当我使用Makefile编译代码时,我得到了错误:test\u default\u ctor:./stack.hpp:53:T stack::pop()[T=String]:断言`TOS=“0”失败 因为我在头文件中声明了pop()函数,所以测试没有运行。以下是我的代码片段:C++ C++-&燃气轮机;断言失败堆栈,c++,stack,assertion,C++,Stack,Assertion,我正在为我的CS课程准备一堆字符串(我自己制作的字符串ADT),但我被抓住了其中的一部分,无法找出哪里出了问题。当我使用Makefile编译代码时,我得到了错误:test\u default\u ctor:./stack.hpp:53:T stack::pop()[T=String]:断言`TOS=“0”失败 因为我在头文件中声明了pop()函数,所以测试没有运行。以下是我的代码片段: #ifndef STACK_HPP #define STACK_HPP #include <iostr
#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;
}
#如果无烟囱#水电站
#定义STACK_水电站
#包括
#包括
#包括
#包括“string/string.hpp”
模板
类节点{
公众:
node():next(0),data(){};
node(const T&x):next(0),data(x){};
节点*下一步;
T数据;
//考题:这个类将被另一个类使用,所以所有的都需要是可访问的
//包括节点p和节点v。
};
模板
类堆栈{
公众:
stack():TOS(0){};
~stack();
堆栈(常量堆栈&);
无效交换(堆栈和);
堆栈和运算符=(堆栈rhs){swap(rhs);返回*this;};
布尔运算符==(常量堆栈和)常量;
bool操作符!=(const stack&rhs)const{return!(*this==rhs);};
friend std::ostream和操作员next;
int leng=slength();
--len;
删除临时文件;
返回结果;
}
模板
布尔堆栈::运算符==(常量堆栈和rhs)常量{
堆栈左=*此;
堆栈右侧=rhs;
如果(slength()!=rhs.slength())
返回false;
if(left.pop()!=right.pop())
返回false;
其他的
返回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;
}
#包括“stack.hpp”
#包括“string/string.hpp”
#包括
int main()
{
{
堆栈测试;
断言(test==stack());
断言(test.slength()==0);
}
std::cout在你发布你的操作符==
代码后,很明显发生了什么。该操作符正在空堆栈上调用pop
。你必须重写操作符==
函数,这样它就不会使用pop
修改堆栈,如果你不想使用它。你应该遍历每个n对堆栈进行ode,并比较它们是否相等
此外,我将删除下面突出显示的两行。这两行复制了堆栈的副本,一般来说,如果不需要,您不想这样做。此外,由于您的类包含指针,您应该正确编写操作符=
。因为代码使用编译器生成的默认操作符=
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;
}
模板
布尔堆栈::运算符==(常量堆栈和rhs)常量{
堆栈左=*此;//删除此
stack right=rhs;//删除此
如果(slength()!=rhs.slength())
返回false;
if(left.pop()!=right.pop())
返回false;
其他的
返回true;
}
我猜你没有显示所有的代码,而且有人在某处调用了pop
。在你说了这些之后后退一步,我觉得这可能是我的equal运算符的问题,所以我在那里编辑了这个运算符,我想这可能是问题所在。请看下面我的答案。啊,谢谢。我在检查它时完全忽略了这些ver.那么你是说我的操作符=在弹出窗口上不正确?我在上面有它,但这是我的:stack&operator=(stack rhs){swap(rhs);return*this;};抱歉,我没有看到你的操作符=
。如果你的swap
和复制构造函数
实现正确,它看起来是正确的。