RUN TIME ERROR

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pranav

    RUN TIME ERROR

    #include <iostream>
    #include <conio.h>
    using namespace std;

    class Base{
    public :
    Base(){ ib = 123; cb = 'B'; }
    int ib;
    char cb;
    virtual void print(){ cout << "Base Class\n" ;}
    ~Base(){ }
    };

    class Der : public Base{
    public :
    Der():Base(){ id = 456; cd = 'D'; }
    int id;
    char cd;
    void print(){ cout << "Deerived Class\n"; }
    ~Der(){ }
    };

    main()
    {
    Der d1, *dp = new Der;
    Base b1, *bp;

    Base *bp2 = dynamic_cast<Ba se *(dp) ;

    bp2->print( );

    // Base *bp3 = dynamic_cast<Ba se *(bp2) ;
    // Der *dp3 = dynamic_cast<De r *(bp2) ;



    getche();
    return 0;
    }

    COMMENTED LINES are generating RUN TIME ERROR
  • peter koch

    #2
    Re: RUN TIME ERROR

    On 11 Aug., 18:11, Pranav <pranav...@gmai l.comwrote:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    >
    class Base{
    public :
            Base(){ ib = 123; cb = 'B'; }
            int ib;
            char cb;
            virtual void print(){ cout << "Base Class\n" ;}
            ~Base(){ }
    >
    };
    >
    class Der : public Base{
    public :
            Der():Base(){ id = 456; cd = 'D'; }
            int id;
            char cd;
            void print(){ cout << "Deerived Class\n"; }
            ~Der(){ }
    >
    };
    >
    main()
    {
            Der d1, *dp = new Der;
            Base b1, *bp;
    >
            Base *bp2 = dynamic_cast<Ba se *(dp) ;
    >
            bp2->print( );
    >
    //    Base *bp3 = dynamic_cast<Ba se *(bp2) ;
    //      Der *dp3 = dynamic_cast<De r *(bp2) ;
    >
    getche();
    return 0;
    >
    }
    >
    COMMENTED LINES are generating RUN TIME ERROR
    I see no errors in your code, so you need to be more specific. What
    happens when uncommenting the lines? Is it a runtime error from the
    compiler or an error when you try to run the program?

    Also your code could improve a lot - e.g. most of the dynamic casts
    are not needed, but I am not going to comment further on that.

    /Peter

    Comment

    • Pranav

      #3
      Re: RUN TIME ERROR

      On Aug 11, 10:42 pm, peter koch <peter.koch.lar ...@gmail.comwr ote:
      On 11 Aug., 18:11, Pranav <pranav...@gmai l.comwrote:
      >
      >
      >
      #include <iostream>
      #include <conio.h>
      using namespace std;
      >
      class Base{
      public :
      Base(){ ib = 123; cb = 'B'; }
      int ib;
      char cb;
      virtual void print(){ cout << "Base Class\n" ;}
      ~Base(){ }
      >
      };
      >
      class Der : public Base{
      public :
      Der():Base(){ id = 456; cd = 'D'; }
      int id;
      char cd;
      void print(){ cout << "Deerived Class\n"; }
      ~Der(){ }
      >
      };
      >
      main()
      {
      Der d1, *dp = new Der;
      Base b1, *bp;
      >
      Base *bp2 = dynamic_cast<Ba se *(dp) ;
      >
      bp2->print( );
      >
      // Base *bp3 = dynamic_cast<Ba se *(bp2) ;
      // Der *dp3 = dynamic_cast<De r *(bp2) ;
      >
      getche();
      return 0;
      >
      }
      >
      COMMENTED LINES are generating RUN TIME ERROR
      >
      I see no errors in your code, so you need to be more specific. What
      happens when uncommenting the lines? Is it a runtime error from the
      compiler or an error when you try to run the program?
      >
      Also your code could improve a lot - e.g. most of the dynamic casts
      are not needed, but I am not going to comment further on that.
      >
      /Peter


      Base *bp3 = dynamic_cast<Ba se *(bp2) ;
      Der *dp3 = dynamic_cast<De r *(bp2) ;

      These two lines are giving error in above code.., When you uncomment
      them will give you run time error,

      Comment

      • James Kanze

        #4
        Re: RUN TIME ERROR

        On Aug 11, 6:11 pm, Pranav <pranav...@gmai l.comwrote:
        #include <iostream>
        #include <conio.h>
        using namespace std;
        >
        class Base{
        public :
        Base(){ ib = 123; cb = 'B'; }
        int ib;
        char cb;
        virtual void print(){ cout << "Base Class\n" ;}
        ~Base(){ }
        >
        };
        >
        class Der : public Base{
        public :
        Der():Base(){ id = 456; cd = 'D'; }
        int id;
        char cd;
        void print(){ cout << "Deerived Class\n"; }
        ~Der(){ }
        >
        };
        >
        main()
        {
        Der d1, *dp = new Der;
        Base b1, *bp;
        >
        Base *bp2 = dynamic_cast<Ba se *(dp) ;
        >
        bp2->print( );
        >
        // Base *bp3 = dynamic_cast<Ba se *(bp2) ;
        // Der *dp3 = dynamic_cast<De r *(bp2) ;
        >
        getche();
        return 0;
        >
        }
        COMMENTED LINES are generating RUN TIME ERROR
        With what compiler? Once I corrected the obvious errors in the
        code (inexistant include file, no return type for main, no such
        function getche), it worked for me. With three different
        compilers. (I suppose that there could be something in
        <conio.hwhich causes dynamic_cast not to work, but I'd be
        surprised.)

        Note that the first dynamic_cast is actually a static_cast (and
        in fact an implicit conversion), and the second is a no-op.

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • Pranav

          #5
          Re: RUN TIME ERROR

          On Aug 12, 2:39 pm, James Kanze <james.ka...@gm ail.comwrote:
          On Aug 11, 6:11 pm, Pranav <pranav...@gmai l.comwrote:
          >
          >
          >
          #include <iostream>
          #include <conio.h>
          using namespace std;
          >
          class Base{
          public :
          Base(){ ib = 123; cb = 'B'; }
          int ib;
          char cb;
          virtual void print(){ cout << "Base Class\n" ;}
          ~Base(){ }
          >
          };
          >
          class Der : public Base{
          public :
          Der():Base(){ id = 456; cd = 'D'; }
          int id;
          char cd;
          void print(){ cout << "Deerived Class\n"; }
          ~Der(){ }
          >
          };
          >
          main()
          {
          Der d1, *dp = new Der;
          Base b1, *bp;
          >
          Base *bp2 = dynamic_cast<Ba se *(dp) ;
          >
          bp2->print( );
          >
          // Base *bp3 = dynamic_cast<Ba se *(bp2) ;
          // Der *dp3 = dynamic_cast<De r *(bp2) ;
          >
          getche();
          return 0;
          >
          }
          COMMENTED LINES are generating RUN TIME ERROR
          >
          With what compiler? Once I corrected the obvious errors in the
          code (inexistant include file, no return type for main, no such
          function getche), it worked for me. With three different
          compilers. (I suppose that there could be something in
          <conio.hwhich causes dynamic_cast not to work, but I'd be
          surprised.)
          >
          Note that the first dynamic_cast is actually a static_cast (and
          in fact an implicit conversion), and the second is a no-op.
          >
          --
          James Kanze (GABI Software) email:james.ka. ..@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
          MS Visual Studio 6.0..,

          Comment

          • Nick Keighley

            #6
            Re: RUN TIME ERROR

            On 12 Aug, 12:28, Pranav <pranav...@gmai l.comwrote:
            On Aug 12, 2:39 pm, James Kanze <james.ka...@gm ail.comwrote:
            On Aug 11, 6:11 pm, Pranav <pranav...@gmai l.comwrote:
            #include <iostream>
            #include <conio.h>
            using namespace std;
            >
            class Base{
            public :
                    Base(){ ib = 123; cb = 'B'; }
                    int ib;
                    char cb;
                    virtual void print(){ cout << "Base Class\n" ;}
                    ~Base(){ }
            >
            };
            >
            class Der : public Base{
            public :
                    Der():Base(){ id = 456; cd = 'D'; }
                    int id;
                    char cd;
                    void print(){ cout << "Deerived Class\n"; }
                    ~Der(){ }
            >
            };
            >
            main()
            {
                    Der d1, *dp = new Der;
                    Base b1, *bp;
            >
                    Base *bp2 = dynamic_cast<Ba se *(dp) ;
            >
                    bp2->print( );
            >
            //    Base *bp3 = dynamic_cast<Ba se *(bp2) ;
            //      Der *dp3 = dynamic_cast<De r *(bp2) ;
            >
            getche();
            return 0;
            >
            }
            COMMENTED LINES are generating RUN TIME ERROR
            >
            With what compiler?  Once I corrected the obvious errors in the
            code (inexistant include file, no return type for main, no such
            function getche), it worked for me.  With three different
            compilers.  (I suppose that there could be something in
            <conio.hwhich causes dynamic_cast not to work, but I'd be
            surprised.)
            >
            Note that the first dynamic_cast is actually a static_cast (and
            in fact an implicit conversion), and the second is a no-op.
            >
            MS Visual Studio 6.0
            I don't bother with that modern stuff... :-)

            This code:

            //*************** **************

            #include <iostream>

            using namespace std;

            class Base{
            public :
            Base(){ ib = 123; cb = 'B'; }
            int ib;
            char cb;
            virtual void print(){ cout << "Base Class\n";}
            ~Base(){ }
            };

            class Der : public Base{
            public :
            Der():Base(){ id = 456; cd = 'D'; }
            int id;
            char cd;
            void print(){ cout << "Deerived Class\n"; }
            ~Der(){ }
            };

            int main()
            {
            Der d1, *dp = new Der;
            Base b1, *bp;

            Base *bp2 = dynamic_cast<Ba se *(dp);

            bp2->print( );

            Base *bp3 = dynamic_cast<Ba se *(bp2);
            Der *dp3 = dynamic_cast<De r *(bp2);

            return 0;
            }
            //*************** **************


            compiled with one warning on Visual C++ 5.0
            (that's *even* crapper than your compiler)
            warning C4101: 'bp' : unreferenced local variable


            It gave no runtime errors and produced this output:
            Deerived Class


            I enabled RTTI and disabled pre-compiled headers in VCC
            (it won't compile without RTTI)

            I suggest you are not posting your actual code


            --
            Nick Keighley

            Comment

            Working...