A couple of killers in the Tableau::delete method:
The function will not compile because of the use of the delete
keyword as the function’s name
elements[index+1]
will allow reading elements[nbElements]
Which may or may not be out of the array’s bounds, but is certainly one more than intended.
Try instead:
template <class T>
void Tableau<T>::remove(size_t index) // size_t is unsigned and prevents input of
// negative numbers. One less test required.
// nbElements should also be made unsigned to match
// function name changed to something legal.
{
assert(index < nbElements); // this also ensures nbElements is > 1
// so nbElements-- will not go out of bounds
nbElements--; // moved above the loop because the index+1 will grab the last element
for( ; index < nbElements; index++) // no need to have an i. Index already does this
{
elements[index] = elements[index+1];
}
}
solved how can I delete the elements of array after read it in C++?