队列模板c++实现

#ifndef queue_h  
#define queue_h 

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>

using namespace std;
template <typename T >class Queue;
template <typename T>
class Item
{
    friend class Queue<T>;
    
    private:
        T x;
        Item *next;
        Item ( const T &p ): x(p), next( 0 ){}
};

template < typename T >
class Queue
{
    private:
        Item<T> *head, * tail;
        void cpy();
    public:
        void pop();
        void destroy();
        void push( const T &tmp);
        
        inline bool empty()
        {
            return (head == NULL);
        }
        
        inline T& front()
        {
            return head->x;    
        }
        
        Queue<T> (): head(0), tail(0) {}
        
        //Queue<T> ( const Queue<T> &p ): head(p.head), tail(p.tail) { cpy(p); }
        //Queue<T>( const Queue<T> &q) :head( q.head ) , tail( q.tail ) { copy_elem( q ); }  
        
        Queue<T> & operator= ( const Queue<T> &);
};

template <typename T>
void Queue<T> :: pop()
{
    if( head == NULL )
        return;
    Item<T> *tmp = head;
    head = head->next;
    delete tmp;
    tmp = NULL;
}

template<typename T>
void Queue<T> :: push( const T &tmp )
{
    Item<T> *p = new Item<T> (tmp);
    if( empty() )
    {
        head = tail = p;
    }
    else
    {
        tail = tail->next = p;
    }
}

template<typename T>
void Queue<T> :: destroy()
{
    while( !empty() )
        pop();
}


//fu zhi
template <typename T>
Queue<T>&  Queue<T> :: operator= (const Queue<T> &c)
{
    for (Item <T> *p = c.head ; p; p = p->next)
    {
        push(p->x);
    }
}

/*

template<typename T>  
    Queue<T>& Queue<T>::operator= (const Queue<T> &q)  
    {  
        //复制队列元素,结束条件是head为空的时候  
        for( QueueItem<T> *p= q.head ; p  ; p = p->next )  
        {  
            push( p->item );  
        }  
          
    }  */

/*template <typename T >
void Queue<T> ::cpy(const Queue<T> &p)
{
    for( QueueItem<T> *p= q.head ; p  ; p = p->next )  
     {  
          push( p->item );  
       }  
}*/

#endif


其中在复制构造函数中,加了一句如下,

//Queue<T>( const Queue<T> &q) :head( q.head ) , tail( q.tail ) { copy_elem( q ); }  

然后就出现错误了,如下,望路过的大神给下知道,感谢

/*--------------------配置: mingw5 - CUI Debug, 编译器类型: MinGW--------------------

检查文件依赖性...
正在编译 C:\Users\Desktop\queue\qq.cpp...
[Error] C:\Users\Desktop\queue\qq.cpp:17:   instantiated from here
[Error] C:\Users\Desktop\queue\queue_h.h:45: error: no matching function for call to `Queue<int>::cpy(const Queue<int>&)'
[Warning] C:\Users\Desktop\queue\queue_h.h:27: note: candidates are: void Queue<T>::cpy() [with T = int]
*/


顺便贴下主函数内容= =

#include "queue_h.h"

Queue<int> q;

int main()
{
	int n;
	while(~scanf("%d", &n))
	{
		q.destroy();
		int a;
		while(n--)
		{
			scanf("%d", &a);
			q.push(a);
		}
		Queue<int> v = q;
		while(!v.empty())
		{
			printf("v.front = %d\n", v.front());
			v.pop();
		}
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值