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