简体   繁体   English

c2955-使用“类”模板需要参数列表。 队列

[英]c2955 - Use of Class template reuires argument list. Queue

I'm trying to code a queue based on a list with templates, but I have a strange error and a don't know what to do whith it. 我正在尝试根据带有模板的列表对队列进行编码,但是我遇到了一个奇怪的错误,并且不知道该怎么办。 I googled that error but I haven't find any answers that would be useful for me. 我搜索了该错误,但没有找到对我有用的答案。 Sorry for my English. 对不起我的英语不好。 Thanks for support. 感谢你的支持。

#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;

}
}
}

You have to specify template arguments when you use a template 使用模板时必须指定模板参数

For example 例如

class Queue{

    Node<T> *top;
    Node<T> *bottom;

or 要么

template<class T> void Queue <T>::push(T obj){

Node<T> *newNode= new Node<T>(obj);

Also if you did not define name null yourself then I think you meant NULL or even better to use nullptr if you compiler supports this keyword. 另外,如果您自己没有定义名称null那么我认为您的意思是NULL或者如果编译器支持此关键字,则最好使用nullptr Otherwise this statement 否则,此声明

top=bottom=null;

is invalid. 是无效的。

Also function main shall have return type int. 函数main也应具有返回类型int。

int main()

And it is not clear why you allocate class Queue in heap instead of simply define it as a local object of main. 还不清楚为什么要在堆中分配Queue类,而不是简单地将其定义为main的本地对象。 For example 例如

Queue<int> a;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM