想实现的功能是讲两个队列中不重复的部分加起来
这是主函数里的代码
Queue a, b;
a.push(1);
a.push(2);
a.push(3);
b.push(3);
b.push(4);
b.push(5);
Queue c, d;
cout << "a + b = ";
c = a + b;
cout<< c. back();
这是实现文件中的代码
template
Queue Queue::operator+(Queue obj)
{
int ahead = head;
Queue ans;
type temp;
while(!isEmpty())
{
ans.push(front());
pop();
}
head = ahead;
while(!obj.isEmpty())
{
temp = obj.front();
obj.pop();
while(!isEmpty())
{
if(temp == front())
{
break;
}
pop();
}
if(isEmpty())
ans.push(temp);
head = ahead;
}
head = ahead;
cout << ans.back() << endl;
return ans;
}
为什么在实现类中调用就没问题,而在主函数中调用,最后一个元素却变成了随机值呢。