Hey, Im having a problem with this programm. It is supoused to have a vetor (pila) that would store all the elements on the try block. When it gets to the "666" element (at the end of the try b lock) it is suppouset to throw an exception (pilallenaexception) but instead of printing "cout<<"No pUedo empilar elem : "<<e.getdato()<<" la pila está \"Llena\""; appears ""This application has requested, the runtime to terminate it in an unusual way. please contact the application`s support team for more information.."
" sorry about my bad english. a "

#include <iostream.h>
#include <conio.h>
#include <exception>

using namespace std;

class pilallenaexception : public exception{
      private:
              int dato;
      public:
             pilallenaexception(int){
                                    
                                    };
             int getdato () {
                 return (dato);
                 }
      };

class pilavaciaexception : public exception{
      private:
              int dato;
      public:
             pilavaciaexception(){
                                    
                                  };
      };

class pila {
      private:
              int capacidad, indice;    // son los datos que pertencen a el objeto de la clase "pila"
              int *datos;              // no se por que se utiliza *
      public:
             pila (int);               //constructor (prototipo)
             pila ();                  //CONSTRUCTOR predeterminado (prototipo)
             ~pila(){                  //destructor //elimina cada componente de la clase
                     delete &capacidad;
                     delete &indice;
                     delete &datos;
                     };        
             void vaciar(){ indice=0;};
             bool estaVacia(){
             return (indice == 0);
                    };
             bool estaLlena(){
             return (indice == capacidad);
                       };
/////////////////////////////////////////////////////////////////////////////////////////////////////////7

              void  mostrar()throw (pilavaciaexception) {
                    if(!estaVacia()){
                                     for(int i=indice-1;i>=0;i--)
                                     cout<<"\nelem = "<<datos[i];
                                     }
                    else throw new pilavaciaexception();
              }
              int desapilar() throw (pilavaciaexception){
                    if(!estaVacia()) return (datos[--indice]);
                    else throw new pilavaciaexception();
              }
              void apilar(int elem) throw (pilallenaexception){
                   if(!estaLlena()) datos[indice++]=elem;
                   else throw new pilallenaexception(elem);
              }
              int cima() throw (pilavaciaexception){
                  if(!estaVacia()) return datos[indice-1];
                  else throw new pilavaciaexception();
              }                                                                                                      

//////////////////////////////////////////////////////////////////////////////////////////////////////////             
             
      };

pila::pila (){                           //constructor (inicializa los valores por default)
           capacidad=10; indice=0;
           datos = new int [capacidad];
           }
pila::pila (int cap){                  //constructor con una capacidad indicada
           capacidad=cap; indice=0;
           datos = new int [capacidad];
           }


int main(int argc, char *argv[])
{
    /////////////////////////////////////////////////////////////////////////////////////////
           int aux;
        pila pila(10);
       try{
         aux=25; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
             aux=-45; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
            aux=-12; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
             aux=67; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
            aux=1; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
             aux=0; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
            aux=-43; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
             aux=89; cout<<"apilando elem "<<aux<<endl<<endl;
            pila.apilar(aux);

            aux = pila.desapilar();
            cout<<"desapilando elem "<<aux<<endl;
            aux = pila.desapilar();
            cout<<"desapilando elem "<<aux<<endl;
            aux = pila.desapilar();
            cout<<"desapilando elem "<<aux<<endl;
            cout<<"Cima: "<<pila.cima();
             aux=564; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
             aux=-987; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
            aux=-10; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
             aux=56; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
            aux=1111; cout<<"apilando elem "<<aux<<endl;
            pila.apilar(aux);
            if(pila.estaLlena()) cout<<"pila llena"<<endl;
            cout<<"mostrando contenido";
            pila.mostrar();
            aux=666; cout<<"\napilando elem "<<aux;
            pila.apilar(aux);
       }
       catch (pilavaciaexception e){
         cout<<"la pila está vacia";
       }
       catch(pilallenaexception e){
                   cout<<"No pUedo empilar elem : "<<e.getdato()<<" la pila está \"Llena\"";

       };

    
    ////////////////////////////////////////////////////////////////////////////////7
    getch();
    return 0;
}

Recommended Answers

All 2 Replies

delete &capacidad;
delete &indice;
delete &datos;

All these are wrong.
a) you don't call delete AT ALL unless you called new.
b) a new [] is matched by a delete []
So delete [ ] datos; would be the only thing needed here.

Ok I found the problem. You are rigth thougth. What solved my problem was deleting new:

void apilar(int elem) throw (pilallenaexception){
                   if(!estaLlena()) datos[indice++]=elem;
                   else throw [U]new[/U] pilallenaexception(elem);
              }
              int cima() throw (pilavaciaexception){
                  if(!estaVacia()) return datos[indice-1];
                  else throw [U]new[/U] pilavaciaexception();

That solved it. thanks...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.