我正在尝试比较两个堆栈,看看它们是否相等。不幸的是,这段代码使得我所有的比较都说堆栈是相等的,即使堆栈不是。
Stack_implementation.cpp
片段:
int DoubleStack::operator==(DoubleStack& rhs)
{
int equal = 1;
for (int i = 0; i <= tos; i++) //tos is the top of the stack indicator
{ //it tells how big the stack is
if (data[i] != rhs.data[i])
{
equal = 0;
break;
}
}
return equal;
}
main.cpp
相关片段:
{
cout << "Comparing stacks..." << endl;
if (stack1 == stack2)
cout << "stack1 = stack2." << endl;
else
cout << "stack1 != stack2." << endl;
}
输出总是
stack1 = stack2
有谁知道怎么了?