2017-04-23 114 views
-2

嗯,我正在做的任务,但不知道我的问题是缺少模板参数的“C++

这是我的任务

说明你有两个环节进行分配。这些部分是相关的,但在实现上有所不同。为了更好地理解任务本身,回头阅读本书,幻灯片,笔记等,以及与Stack ADT一起执行常规基于阵列和链接列表的堆栈可能会有所帮助。

第一部分 一种广泛使用的堆栈是从许多不同的应用提供了撤消操作,为我们所熟悉。尽管支持撤销可以用无界的堆栈实现(只要内存允许,堆栈会不断增长和增长),但许多应用程序对这种撤消历史记录提供的支持有限。换句话说,堆栈是固定容量的。 当这样的堆栈已满,并且推被调用,而不是抛出异常,更典型的方法是在顶部接受推元件,同时从堆的底部移除最旧的元件,以腾出空间。这被称为“泄漏”。请注意,这并不意味着ADT公开了一种允许直接从底部删除的方法。这仅在堆栈变满时执行。 对于这部分,您将使用一些基于阵列的实现来实现这种LeakyStack抽象。 请注意,您必须创建一个Leaky Stack接口,然后使用C++:运算符来使用LeakyArrayStack实现来实现该接口(使用公共继承)。请参阅分配说明末尾附近指定的接口。

第二部分重复第一部分,但是为实际的数据存储使用单向链表而不是数组,并且允许为构造函数指定一个最大容量。

注:•无论是基于阵列和链表基于漏栈应该使用相同的LeakyStackInterface,规定如下。请记住 - 这是一个LeakyStack ADT。它指定了LeakyStack的作用,而不是如何。所以,为了提供一个实现,接口不应该有所不同。 •在两部分中使用公共继承•在尝试执行第II部分之前,您应该先编写一个SinglyLinkedList类然后使用包含(聚合或合成,一种关系)来实现部分II

I GOT TO使用该界面中的图片

这是我的代码

#include <iostream> 
    #ifndef LEAKYStacksINTERFACE 
    #define LEAKYStacksINTERFACE 
     #define cap 10 
    using namespace std; 



    template<typename ItemType> 
    class LeakyStacksInterface 
     { public: 
     //returns whether Stacks is empty or not 
    virtual bool isEmpty() const = 0; 

    //adds a new entry to the top of the Stacks 
    //if the Stacks is full, the bottom item is removed 
    //or "leaked" first, and then the new item is set to the top 
    //---> If the Stacks was full when the push was attempted, return false 
    //---> If the Stacks was not full when the push was attempted, return true 
    virtual bool push(const ItemType& newEntry) = 0; 

    //remove the top item 
    //if the Stacks is empty, return false to indicate failure 
    virtual bool pop() = 0; 

     //return a copy of the top of the Stacks 
    virtual ItemType peek() const = 0; 

    //destroys the Stacks and frees up memory 
    //that was allocated 
    // virtual ~StacksInterface() {} 
    }; 

template<typename ItemType> 
struct node 
{ 
    int data; 
    struct node *next; 
}; 




template<typename ItemType> 
class Stacks : public LeakyStacksInterface<ItemType> 
{ 

    struct node<ItemType> *top; 

    public: 
      int size; 
      ItemType *myArray; 

    Stacks() 
    { 
     top=NULL; 
     size = 0; 
     myArray = new ItemType[cap]; 
    } 

    ~Stacks() { 
     size = 0; 
    } 

    public: 
    // pure virtual function providing interface framework. 
    bool isEmpty() const { 

     return(size == 0); 

    } 
bool push(const ItemType& newEntry) { 
     if(size == cap) { 
      for(int i = 0; i < size-1; i++) { 
       myArray[i] = myArray[i+1]; 
      } 
      myArray[size-1] = newEntry; 
      return false; 
     } 
} 
    ItemType peek() const { 
     return myArray[size-1]; 
    } 
    void display() 
    { 
     cout<<"Stacks: [ "; 
     for(int i=size-1; i>=0; i--) 
     { 
     cout<<myArray[i]<<" "; 
     } 
     cout<<" ] "<<endl; 
    } 
}; 


int main() 
{ 
    Stacks s; 

    int choice; 

    while(1) 

    { 

     cout<<"n-----------------------------------------------------------"; 

     cout<<"nttSTACK USING LINKED LISTnn"; 

     cout<<"1:PUSHn2:POPn3:DISPLAY STACKn4:EXIT"; 

     cout<<"nEnter your choice(1-4): "; 

     cin>>choice; 

     switch(choice) 

     { 

      case 1: 

       s.push(); 

       break; 

      case 2: 

       s.pop(); 

       break; 

      case 3: 

       s.show(); 

       break; 

      case 4: 

       return 0; 

       break; 

      default: 

       cout<<"Please enter correct choice(1-4)!!"; 

       break; 

     } 

    } 

    return 0; 
} 

#endif 

这里是我的错误: 错误:之前的“ 错误缺少模板参数:预期“;” ' 错误:'s'在此范围内未被挖空

请帮忙! 谢谢!

INTERFACE PICTURE

+0

栈是一个模板,它需要一个自变量,如堆栈秒。 – didiz

回答

2

Stacks是一个类模板,所以使用它,你必须提供一个模板参数,就像

Stacks<int> s; 
+0

我做了,当我把Stacks s给了我更多的错误; – user5717696

+0

@ user5717696然后还有其他错误。可能在你模板实现。通过实例化一个'Stacks'对象,你需要提供一个类型。这是你问到的错误。 – Persixty

+0

使用colngu和clang ++或g ++作为编译器,你会得到比MVSC更好的错误...''Stacks'中未实现的纯虚方法'pop'([link](http://coliru.stacked-crooked的.com /一个/ 0f44ef9fab980906)) – Asu