#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啊?实在是搞不懂
Node的构造函数第一个参数为Person,你传NULL进去肯定不行啊。改为new Node<T>(Person("",0),NULL);试试
这一行this->head = new Node<T>(NULL,NULL);改为this->head = new Person(NULL,NULL);
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y