#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
template<class T>
class Queue
{
public:
Queue()
{
this->head = new Node<T>(NULL,NULL);
this->last = NULL;
this->N = 0;
}
bool isEmpty(){return N == 0;}
int size(){return N;}
T pop()
{
if (this->size()==0)
{
return NULL;
}
Node<T>* oldfirst = head->next;
head->next = oldfirst->next;
N--;
if (this->isEmpty())
{
last = NULL;
}
return oldfirst->item;
}
void push(T t)
{
if (this->size() == 0)
{
this->head->next = new Node<T>(t, NULL);
}
else
{
Node<T>* newfirst = new Node<T>(t, head->next);
head->next = newfirst;
}
N++;
}
template<class T>
class Node
{
public:
Node(T i,Node* n)
{
this->item = i;
this->next = n;
}
T item;
Node<T>* next;
};
int N;
Node<T>* head;
Node<T>* last;
};
void main()
{
Person p1("唐僧", 30);
Person p2("孙悟空", 1000);
Person p3("猪八戒", 900);
Person p4("沙僧", 800);
Queue<Person> q;
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
while (q.size() != 0)
{
cout << q.pop().m_Name << " " << q.pop().m_Age << endl;
}
/*Queue<char> q;
q.push('1');
q.push('d');
q.push('3');
q.push('f');
q.push('3');
while (q.size() != 0)
{
cout << q.pop() << endl;
}*/
cout << endl;
system("pause");
return;
}
使用自定义的类放入队列时出错,错误列表: return无法从“int” 转为“Person”,请问如何解决?第24行this->head = new Node<T>(NULL,NULL); 也出现同样错误 :参数1无法从“int” 转为“Person”,可是明明参数1 是模板T啊?实在是搞不懂