I've written a program. It doesn't work. I am hoping for just overloading errors and maybe passing arguments by value when it should be by reference, but I'm just not finding the problems!
I'm not asking for anyone to fix it for me, just for someone to show me where I've gone wrong.
Thank you.
Here are my function definitions. If you see an obvious problem with anything PLEASE let me know.
//**************************************************************************
voidoperator +(list a, list b)
// Precondition:
// Postcondition: Overloads (+) operator; Adds two arrays together, as long
// as total number of elements will not exceed CAPACITY.
{
for (int i = 0; i < b.size(); i++)
{
size_t sz = a.mySize;
a.myList[sz] = b.myList[i];
sz++;
}
}
//**************************************************************************
ostream& operator <<(ostream& outs, list& a)
// Precondition: none.
// Postcondition: Overloads the (<<) operator.
{
for (int i = 0; i < a.CAPACITY; i++)
outs << a.myList[i] << endl;
}
//**************************************************************************
bool list::operator ==(list secondList)
// Precondition: There are two lists to compare.
// Postcondition: Each list has been compared element by element until at
// least one inconsistency was found. If none were found, the lists are
// equal.
{
if(pos == secondList.pos)
{
if(mySize == secondList.mySize)
{
returntrue;
}
}
elsereturnfalse;
}
//**************************************************************************
list::list()
// Precondition: None
// Postcondition: A new list has been created, and the position set to 0.
{
mySize = 0;
for (pos = 0; pos < CAPACITY; pos++)
{
myList[pos] = 0;
}
// Set position back to beginning.
pos = 0;
}
//**************************************************************************
list::list(const list& list1)
// Precondition: There is at least 1 list created already.
// Postcondition: The second list copies all values in first list.
{
for (pos = 0; pos < mySize; pos++)
{
myList[pos] = list1.myList[pos];
}
}
//**************************************************************************
void list::front()
// Precondition: None.
// Postcondition: Current position of array has been set to first position.
{
pos = 0;
}
//**************************************************************************
void list::end()
// Precondition: Array is not empty.
// Postcondition: Position is set to the last available slot in array.
{
if(mySize != 0)
{
pos = (CAPACITY -1);
}
else
cout << "This is a blank array." << endl;
}
//**************************************************************************
void list::next()
// Precondition: As long as we are not at last array slot.
// Postcondition: Have advanced to next array slot.
{
if((pos + 1) < CAPACITY)
{
pos++;
}
else
cout << "You are already at the end of this array." << endl;
outfile << "You are already at the end of this array." << endl;
}
//**************************************************************************
void list::prev()
// Precondition: Array has at least 2 numbers currently.
// Postcondition: Array position has been set to the previous slot.
{
assert(mySize > 2);
myList[pos - 1];
}
//**************************************************************************
int list::getPos()
// Precondition: None.
// Postcondition: Returns current position of array.
{
return pos;
}
//**************************************************************************
void list::setPos(int p)
// Precondition: None.
// Postcondition: Position in array has been set to passed in number.
{
myList[pos] = p;
}
//**************************************************************************
void list::insertBefore(value_type num)
// Precondition: List is not empty.
// Postcondition: New number has been inserted before current position.
{
assert(mySize != 0);
{
myList[pos - 1] = num;
}
}
//**************************************************************************
void list::insertAfter(value_type num)
// Precondition: Current position is not the last available position of array.
// Postcondition: Num has been inserted after the current array position.
{
myList[pos +1] = num;
}
//**************************************************************************
void list::replace(int num)
// Precondition: None.
// Postcondition: Num has replaced value in current array position.
{
myList[pos] = num;
}
//**************************************************************************
void list::erase()
// Precondition: None.
// Postcondition:Erase value in current position;shuffle array leftward
{
myList[pos] = myList[pos + 1];
// Move to the right one spot in array
pos++;
mySize--;
for (int i = pos; pos < mySize; pos++)
{
myList[i] = myList[i + 1];
}
}
//**************************************************************************
void list::clear()
// Precondition: There is at least 1 value in the list.
// Postcondition: List has been cleared out to zeroes.
{
// Test precondition
assert (mySize != 0);
for ( int i = 0; i < CAPACITY; i++)
{
myList[i] = 0;
}
}
//**************************************************************************
void list::reverse()
// Precondition: List is not empty.
// Postcondition: List has been reversed inside array.
{
// Test precondition
assert(mySize != 0);
for (int i = 0; i < mySize; i++)
{
// Create temp variable to hold value
int temp = myList[i];
myList[i] = myList[mySize - 1];
myList[mySize - 1] = temp;
mySize--;
}
}
//**************************************************************************
void list::swap(list swapList)
// Precondition: There are two non-empty lists.
// Postcondition: The arrays have been swapped out with each other.
{
// Test precondition
assert(mySize != 0 && swapList.mySize != 0);
// Create temporary holder
list temp;
// Swap out current list to temp list
for (int i = 0; i < mySize; i++)
{
temp.myList[i]= myList[i];
}
// Replace current list with other list
for (int i = 0; i < max(mySize,swapList.mySize); i++)
{
myList[i] = swapList.myList[i];
}
// Replace second list with temp list (first list)
for (int i = 0; i < max(mySize, temp.mySize); i++)
{
swapList.myList[i] = temp.myList[i];
}
}
//**************************************************************************
bool list::is_empty()
// Precondition: None.
// Postcondition: Returns true if list is empty; false otherwise.
{
if (mySize == 0)
returntrue;
elsereturnfalse;
}
//**************************************************************************
int list::getElement()
// Precondition: List is not empty.
// Postcondition: Value of current array position is returned.
{
assert(mySize != 0);
return myList[pos];
}
//**************************************************************************
size_t list::size()
// Precondition: None.
// Postcondition: Returns size of array; could be 0 if array is empty.
{return mySize;}
I see a few potential problems:
1. Shouldn't operator+ and operator<< be part of class list?
2. operator+ can't return void
Those were the immediately obvious ones.
What does the compiler say? I can't compile it myself without a definition of class list.