Lesson 1. Catch by reference, not by value.
http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.7
Lesson 2. Do not use operator new when throwing exceptions. The syntax will seem strange to you but you will need to learn how to do this correctly in order to avoid memory leaks.
http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.6
The destructor is almost totally wrong.
1 2 3 4 5
|
~pila(){ //destructor //elimina cada componente de la clase
delete &capacidad;
delete &indice;
delete &datos;
};
|
capacidad and indice were not allocated using operator new therefore you do not delete them. This will cause major run-time problems if you delete this object.
datos was created using operator new but you must not delete using the & symbol. It should be
delete datos;
It looks to me like you are going to have to spend some time understanding the use of operator new in C++ because it is totally different than in Java. There is no garbage collection and unlike java, C++ has pointers. Operator new returns a pointer so you have to take special care in properly allocating and deallocating memory. In this case, I would have made datos like this:
1 2 3 4 5
|
std::vector<int> datos;
pila::pila (int cap)
: capacidad(cap), indice(0), datos(cap)
{
}
|
See std::vector here:
http://cplusplus.com/reference/stl/vector/
As far as what is causing the exception, you should step into the code with a debugger to see if the last function call is causing it or if the pila object causes it during its destruction. It looks like you are checking for invalid indices in the functions. The destructor is definitely wrong and when your object is destroyed, undefined behavior will result.