c2955 - 使用类模板 reuires 参数列表.队列



我正在尝试根据带有模板的列表编写队列,但是我有一个奇怪的错误,并且不知道该怎么办。我在谷歌上搜索了这个错误,但我没有找到任何对我有用的答案。对不起我的英语。感谢您的支持。

#include <iostream>
#include <conio.h>
using namespace std;

template<class T>
class Node{
public: 
    Node(T obj){value=obj; next=NULL;};
Node* next;
T value;
};
template<class T>
class Queue{
    Node *top;
    Node *bottom;
public:
    Queue();
    void push(T obj);
    T pop();
     void print();
    ~Queue(){ if(next) delete next;};
    void delete_queue();


};
template<class T>Queue <T>::Queue(){
    top=bottom=NULL;
}


template<class T> void Queue <T>::push(T obj){
Node *newNode= new Node(obj);
if(bottom) bottom->next = newNode;
else { top=newNode; bottom=newNode;}   
}


    template<class T> T Queue <T>::pop(){
if(top){
    Node * del = top;
    T val = del-> value;
    top=top->next;
    delete del;
    cout<<"popped "<<val;
    return val;
}else {cout<<"Error"; return 0;}
    }

void main(){
int n=0, p;
char k;
Queue<int> *a=new Queue<int>();
while(1){
 cout<<"1.Push n";
 cout<<"2.Pop n";
 k=getch();
 switch(k){
      case '1':
          cout<<"Enter obj "<<endl;
          cin>>p;
          cout<<endl;
          a->push(p);
         break;
      case '2':
        a->pop();
         break;
}
}
}

使用模板时必须指定模板参数

例如

class Queue{
    Node<T> *top;
    Node<T> *bottom;

template<class T> void Queue <T>::push(T obj){
Node<T> *newNode= new Node<T>(obj);

另外,如果您没有自己定义名称null那么我认为如果您编译器支持此关键字,那么您的意思是NULL甚至更好地使用nullptr。否则此声明

top=bottom=null;

无效。

此外,函数 main 应具有返回类型 int。

int main()

并且不清楚为什么在堆中分配类队列,而不是简单地将其定义为 main 的本地对象。例如

Queue<int> a;

最新更新