堆栈继承

Stack Inheritance

本文关键字:继承 堆栈      更新时间:2023-10-16

我刚刚完成了一个使用堆栈的Post-Fix Notation(RPN)项目。我在一些编译器错误方面运气不太好,所以我来找你们,同时我也在一边调试。

这个文件给了我们。我们假设从中派生一个堆栈类。

#ifndef ABSTRACTSTACK_H
#define ABSTRACTSTACK_H
#include <string>
using namespace std;
/* ---------------  Class 'Oops' ---------------
Class 
Thrown when an error is encountered.
Member 'm_msg' stores an error message.
*/
class Oops
{
string m_errormsg;
public:
Oops(string msg) : m_errormsg(msg) {}
const string& getMsg() const
{
return m_errormsg;
}
};

/* ---------------  Abstract Class AbstractStack --------------- */
template < typename T >
class AbstractStack
{
public:
// Purpose: Checks if a stack is empty
// Returns: 'true' if the stack is empty
//     'false' otherwise
virtual bool isEmpty() const = 0;

// Purpose: looks at the top of the stack
// Returns: a const reference to the element currently on top of the stack
// Exception: if the stack is empty, THROW a 'Oops' object with an error message!!!
virtual const T& top() const throw ( Oops ) = 0;
// Purpose: push an element into the stack
// Parameters: x is the value to push into the stack
// Postconditions: x is now the element at the top of the stack,
virtual void push(const T& x) = 0;
// Purpose: pop the stack
// Postconditions: the element formerly at the top of the stack has
// been removed
// Note: Poping an empty stack results in an empty stack.
virtual void pop() = 0;

// Purpose: clears the stack
// Postconditions: the stack is now empty
virtual void clear() = 0;
};
#endif

这是我的派生类及其实现。

#ifndef STACK_H
#define STACK_H
#include<string>
#include<iostream>
#include<cstdlib>
#include "abstractstack.h"
using namespace std;

template<typename T>
struct Node
{
T Data;
Node<T>* next;
};

template<typename T> 
class Stack : public AbstactStack<T>
{
private:
Node<T>* Top;
public:
//Purpose: Destructor
//Postconditions: The stack is now empty
~Stack() {};
//Purpose: Default Constructor
//Postconditions: Top is initialized to 'NULL'
Stack(); Top(NULL){};
//Overloaded = Operator
//Postconditions: *this is now equal to rhs
const Stack<T>& operator = (const Stack<T>& rhs);

//Purpose: Check if a stack is empty
//Returns: 'true' if the stakc is empty, 'false' otherwise
bool isEmpty() const;
//Purpose: Looks at the top of the stack
//Returns: a const reference to the element currently on top of the stack
//Exception: if the stack is empty, THROW a 'Oops' object with an error message!!!"
const T& top() const throw(Oops);
//Purpose: push an element into the stack
//Parameters: x is the value to push into the stack
//Postconditions: x is now the element at the top of the stack
void push(const T& x);
//Purpose: pop the stack
//Postconditions: the element formerly at the top of the stack has been removed
//Popping an empty stack results in an empty stack
void pop();
//Purpose: clears the stack
//Postconditions: the stack is now empty
void clear();
//Reverses the stack
//Postconditions: stack is now in reverse order
void reverse();
};
#include "stack.hpp"
#endif

实施(.hpp)

#include <string>
#include <iostream>
using namespace std;

template<typename T>
const Stack<T>& Stack<T>::operator = (const Stack<T>& rhs)
{
if (this != &rhs)
{
if (Top != NULL)
clear();
Node<T>* rhsPtr = rhs.Top;
Node<T>* copyPtr = Top = new Node<T>;
copyPtr->Data = rhsPtr->Data;
while (rhsPtr->next != NULL)
{
rhsPtr = rhsPtr->next;
copyPtr->next = new Node<T>;
copyPtr = copyPtr->next;
copyPtr->Data = rhsPtr->Data;
}
copyPtr->next = NULL;
}
return(*this)
}
template<typename T>
Stack<T>::Stack(const Stack<T>& rhs)
{
Top = NULL;
*this = rhs;
}
template<typename T>
bool Stack<T>::isEmpty()
{
return(Top == NULL);
}
template<typename T>
const T& top() const throw(Oops)
{
if (Top != NULL)
return(Top->Data);
else
throw Oops(Stack is Empty!);
}
template<typename T>
void Stack<T>::push(const T& x)
{
Node<T>* newNode = new Node;
newNode->Data = x;
if (isEmpty())
{
Top = newNode;
return;
}
newNode->next = Top;
Top->next = newNode;
}
template<typename T>
void Stack<T>::pop()
{
Node<T>* temp;
if (Top != NULL)
{
temp = Top;
Top = Top->next;
delete temp;
}
}
template<typename T>
void Stack<T>::clear()
{
Node<T>* temp;
while (Top != NULL)
{
temp = Top;
Top = Top->next;
delete temp;
}
}
template<typename T>
void Stack<T>::reverse()
{
Node<T>* current;
Node<T>* previous;
Node<T>* next;
previous = NULL;
current = Top;
while (current != NULL)
{
next = current->next;
current->next = previous;
previous = current;
current = next;
}
Top = previous;
}

编译器对我的Stack类不满意。"stack.h:25:34:错误:在'<'标记之前需要模板名称class堆栈:public AbstractStack

有人告诉我,我不能从Abstract继承,因为它不是一个类,但我的教授告诉我这很好,我应该在Stack的.cpp中添加一个forward声明,但我不确定这是怎么回事。我知道我还远远没有完成,但我希望至少能够解决这个错误,这样我就可以看看我的程序是否正常工作。

我在Pastebin中发布了我的主程序,并在这里提供了它的标题和实现,以备不时之需。http://pastebin.com/1t2YGa2c

在我看来,您遇到的问题只是以下行中的一个拼写错误:

class Stack : public AbstactStack<T>

AbstactStack<T>更改为AbstractStack<T>,这应该会起作用。

您收到的错误解释说,字符<之前的类名应该是一个模板,但由于您在类名中拼写错误,它无法识别。

请确保您阅读并理解错误消息!在解决这样的问题时,它们通常非常有帮助。

小提示:如果你查看错误消息,你会看到错误所在的文件名,然后是行号和列号(stack.h:25:34)。调试时非常有用的信息。