Я только что закончил проект для Post Fix Notation (RPN), используя стек. Мне не очень повезло с некоторыми ошибками компилятора, поэтому я прихожу к вам, ребята, пока я работаю над отладкой на моей стороне.Stack Inheritance
Этот файл предоставлен нам. Предположим, что он выводит из него класс стека.
#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: ошибка: ожидается шаблон имя перед„<“маркера класса Stack: общественный AbstactStack
Мне сказали, что я не могу наследовать из реферата, потому что это не класс, а мой профессор говорит мне, что все в порядке, и что я должен добавить форвардную декларацию в свой .cpp Stack, но я не уверен, как это происходит. Я знаю, что я далек от завершения, но я хотел бы решить хотя бы эту ошибку так что я могу видеть, если моя программа работает правильно или нет.
Я отправил свою основную программу в Pastebin с заголовками и реализации здесь, в случае, если это необходимо. http://pastebin.com/1t2YGa2c
Вы только что опечалили: 'AbstactStack'. –