c++ - c2955 - 使用类模板需要参数列表。队列

标签 c++ list templates queue

我正在尝试根据带有模板的列表编写队列代码,但我遇到了一个奇怪的错误并且不知道如何处理它。我用谷歌搜索了那个错误,但没有找到任何对我有用的答案。 对不起我的英语不好。 感谢支持。

#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()

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

Queue<int> a;

关于c++ - c2955 - 使用类模板需要参数列表。队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21764304/

相关文章:

c++ - 假设指针是一个结构

c# - C++ 非托管包装器在 Web 项目/iis 中不起作用

list - 挑战 : optimize unlisting [easy]

c++ - 如何在 C++ 中实现公式模式?

C++:为什么这个 constexpr 不是编译时常量

c++ - 连续数组多维表示的快速元素访问

c++ - BLAS 是如何获得如此极致的性能的?

没有初始化的 C++ 数组成员

列表项的python连接组合

list - haskell 递增对的无限列表