堆栈继承

时间:2015-04-21 22:55:08

标签: c++ inheritance stack

我刚刚使用堆栈完成了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:错误:在'&lt;'标记之前预期的模板名称  class Stack:public AbstactStack

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

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

1 个答案:

答案 0 :(得分:3)

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

class Stack : public AbstactStack<T>

AbstactStack<T>更改为AbstractStack<T>,这应该有效。

您获得的错误解释了字符<之前的类名应该是模板,但由于您在类的名称中输入了错字,因此无法识别。

确保您阅读并理解错误消息!在解决这些问题时,它们通常非常有用。

小方注意:如果您查看错误消息,则会看到错误所在的文件名,后跟行号和列号(stack.h:25:34)。调试时非常有用的信息。