Originally Posted by
nvoigt
A problem persists... why would you return the list after inserting ? Why not return a bool ? You clearly have conditions in your function where inserting fails. Why hide that from the user ?
Return true is it was inserted, false if the list was full.
Check the returnvalue in your main program
thanx nvoigt i follows your advice and it was now fixed, here is the code:
Code:
bool LinearList<T>::insert(int pos, T ele)
{
if (!isFull())
{
for (int i=__length-1; i>=pos; i--)
{
__elements[i+1] = __elements[i];
}
__elements[pos] = ele;
__length++;
return true;
}
return false;
}
why to return *this in previous one is becuase the example of my book did so, here is the original piece of code and i still didnt know the reason for returning *this
Code:
template <class T>
LinearList<T>& LinearList<T>::Insert(int k, const T& x)
{
if (k < 0 || k > length) throw OutOfBounds();
if (length == MaxSize) throw NoMem();
for (int i=length-1; i>=k; i--)
{
element[i+1] = element[i];
}
element[k] = x;
length++;
return *this;
}
Regards~