C++ -> 断言失败堆栈
C++ -> Assertion failure Stack
我正在为我的 CS class 获取一堆字符串(我自己制作的字符串 ADT),但我被其中的一部分卡住了,无法弄清楚哪里出了问题。当我使用 Makefile 编译代码时,出现错误:test_default_ctor: ./stack.hpp:53: T stack::pop() [T = String]: Assertion `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
修改栈你不想用吗。您应该遍历堆栈的每个节点并比较它们是否相等。
此外,我会删除下面突出显示的两行。那就是制作 副本 你的堆栈,一般来说,如果不需要,你不想这样做。此外,由于您的 class 包含指针,因此您应该正确编写 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;
}
我正在为我的 CS class 获取一堆字符串(我自己制作的字符串 ADT),但我被其中的一部分卡住了,无法弄清楚哪里出了问题。当我使用 Makefile 编译代码时,出现错误:test_default_ctor: ./stack.hpp:53: T stack::pop() [T = String]: Assertion `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
修改栈你不想用吗。您应该遍历堆栈的每个节点并比较它们是否相等。
此外,我会删除下面突出显示的两行。那就是制作 副本 你的堆栈,一般来说,如果不需要,你不想这样做。此外,由于您的 class 包含指针,因此您应该正确编写 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;
}