2 つのスタックを比較して、それらが等しいかどうかを確認しようとしています。残念ながら、このコードでは、スタックが等しくなくても、すべての比較でスタックが等しいと言われています。
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
誰が何が悪いのか知っていますか?