I have an error in my function and cannot figure out how to fix it. I've been working with it, but still no go. I'm trying to compare the contents of two arrays. If the size of the arrays are not equal then it returns false, else if they are the same size then it is supposed to compare each element of the array for equality. When it finds something that is not equal then it is supposed to return false and set the positions back to their original starting points. When the lists are populated, list a contains 1-20 and list b contains 20-1, so they are not equal but it always returns true. Any tips, help, or suggestions is greatly appreciated!
// overloaded == function
booloperator==(list & a,list & b)
{
int p1=a.getPos(); // stores list a position
int p2=b.getPos(); // stores list b position
int c=0; // using a variable to move position down the array
a.setPos(c); // sets a position
b.setPos(c); // sets b position
//Check array size
if(a.size()!=b.size())
{
returnfalse;
}
for(int i=0;i<a.size();i++)
{
if(a.getElement()!=b.getElement())
{
returnfalse;
c++; // bumps c
a.setPos(c); // sets list a position to c
b.setPos(c); // sets list b position to c
}
}
a.setPos(p1); //sets a position back to original position
b.setPos(p2); //sets b position back to original position.
returntrue; // if all is equal then return true.
}
for(int i=0;i<a.size();i++)
{
if(a.getElement()!=b.getElement())
{
returnfalse;
c++; // bumps c
a.setPos(c); // sets list a position to c
b.setPos(c); // sets list b position to c
}
}
is invalid because the statements
1 2 3
c++; // bumps c
a.setPos(c); // sets list a position to c
b.setPos(c); // sets list b position to c
will be never executed I think you should check a.size(), a.getElement() and b.getElement(). Insert statements that output these values
1 2 3 4 5 6 7 8 9 10 11 12
cout << "size of a = " << a.size() << endl;
for(int i=0;i<a.size();i++)
{
std:;cout << "a = " << a,getElement() << ", b = " << b.getElement() << endl;
if(a.getElement()!=b.getElement())
{
returnfalse;
}
c++; // bumps c
a.setPos(c); // sets list a position to c
b.setPos(c); // sets list b position to c
}
for(int i=0;i<a.size();i++)
{
if(a.getElement()!=b.getElement())
returnfalse;
c++; // bumps c
a.setPos(c); // sets list a position to c
b.setPos(c); // sets list b position to c
}