pure virtual method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mfabricius@gmail.com

    pure virtual method

    Hi,

    I am probably trying to do something really stupid.

    There is a class shape:

    class Shape
    {
    public:
    Shape();
    virtual ~Shape();
    ....
    virtual bool inside(prec_t x, prec_t y) = 0;
    ....
    virtual Shape * copy() = 0;
    ....
    };

    and a class circle which is derived from shape

    class Circle : public Shape
    {
    public:
    Circle(prec_t radius);
    Circle(prec_t radius, prec_t outerRadius);
    Circle();
    virtual ~Circle();

    ....
    bool inside(prec_t x, prec_t y);
    ....
    Shape * copy();
    ....
    };

    ok, now the class Surface has a member of type Shape *:

    class Surface
    {
    public:
    ....
    Shape* myShape;
    ....
    }


    finally the last class derives from Surface:
    class EvenASph : public Surface
    {
    public:
    ....
    Surface outerShell;
    Surface lowerShell;
    ....
    }

    now in this class I am trying to do something like

    1) upperShell.shap e = this->shape->copy();
    2) lowerShell.shap e = this->shape->copy();

    this seems to work, the compiler does not complain.
    But I occasionally encountered crashes when I call
    upperShell.shap e->inside()

    Occasionally meaning, either it does not work at all -
    the program immediately causes a Segmentation fault -
    or it just work fine and never crashes.
    I used the compiler g++ 4.0.3.


    If instead of 1 and 2) I do, for example
    lowerShell.shap e = this->shape

    I always get an error when trying to execute:

    pure virtual method called
    terminate called without an active exception


    Am I trying to do something really illegal?



    Thanks,

    Maximilian

  • Ian Collins

    #2
    Re: pure virtual method

    mfabricius@gmai l.com wrote:
    >
    I always get an error when trying to execute:
    >
    pure virtual method called
    terminate called without an active exception
    >
    >
    Am I trying to do something really illegal?
    >
    You aren't doing something daft like calling a virtual method from a
    constructor are you?

    --
    Ian Collins.

    Comment

    • Victor Bazarov

      #3
      Re: pure virtual method

      mfabricius@gmai l.com wrote:
      I am probably trying to do something really stupid.
      [..]
      I always get an error when trying to execute:
      >
      pure virtual method called
      terminate called without an active exception
      >
      >
      Am I trying to do something really illegal?
      Calling a pure virtual function is not illegal, it just has
      undefined behaviour.

      In addition to other's suggestions, check if any of your objects
      are passed by value instead of by reference. That would cause
      slicing. Basically, put a breakpoint in the constructor of your
      abstract base class and see if it's ever called *not* from the
      constructor of the derived class.

      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask


      Comment

      • mfabricius@gmail.com

        #4
        Re: pure virtual method

        On 27 Aug., 10:21, Ian Collins <ian-n...@hotmail.co mwrote:
        mfabric...@gmai l.com wrote:
        >
        I always get an error when trying to execute:
        >
        pure virtual method called
        terminate called without an active exception
        >
        Am I trying to do something really illegal?
        >
        You aren't doing something daft like calling a virtual method from a
        constructor are you?
        >
        --
        Ian Collins.
        Hi all,

        by implementing the example, I think I found my mistake.

        Thanks for the replies,

        Maximilian

        Comment

        • anon

          #5
          Re: pure virtual method

          mfabricius@gmai l.com wrote:
          On 27 Aug., 10:21, Ian Collins <ian-n...@hotmail.co mwrote:
          >mfabric...@gma il.com wrote:
          >>
          >>I always get an error when trying to execute:
          >> pure virtual method called
          >> terminate called without an active exception
          >>Am I trying to do something really illegal?
          >You aren't doing something daft like calling a virtual method from a
          >constructor are you?
          >>
          >
          Hi all,
          >
          by implementing the example, I think I found my mistake.
          >
          So, what was the mistake?

          Comment

          • BobR

            #6
            Re: pure virtual method


            <mfabricius@gma il.comwrote in message...
            Hi,
            I am probably trying to do something really stupid.
            There is a class shape:
            >
            class Shape{ public:
            Shape();
            virtual ~Shape();
            ...
            virtual bool inside(prec_t x, prec_t y) = 0;
            ...
            virtual Shape * copy() = 0;
            ...
            };
            You might want to 'define' those pure virtuals for testing.

            bool Shape::inside( prec_t x, prec_t y){
            std::cerr<<"bas e virtual \"Shape::inside ()\" called"<<std::e ndl;
            return false;
            }

            Shape* Shape::copy(){
            std::cerr<<"bas e virtual \"Shape::copy() \" called"<<std::e ndl;
            return this;
            }

            ..... because of this:

            class Surface{ public: ...
            Shape* myShape; // <--------! slice if set to a derived?
            }; // ADD the semicolon!!!

            >
            1) upperShell.shap e = this->shape->copy();
            There is no 'upperShell' in the incomplete code you showed.
            There is no 'shape' in the incomplete code you showed.
            ( did you mean 'myShape'? )

            You also failed to show the definitions for:
            Circle::inside( )
            Circle::copy()

            FAQ http://www.parashift.com/c++-faq-lite

            --
            Bob R
            POVrookie


            Comment

            • mfabricius@gmail.com

              #7
              Re: pure virtual method

              On Aug 27, 4:02 pm, anon <a...@no.nowrot e:
              mfabric...@gmai l.com wrote:
              On 27 Aug., 10:21, Ian Collins <ian-n...@hotmail.co mwrote:
              mfabric...@gmai l.com wrote:
              >
              >I always get an error when trying to execute:
              > pure virtual method called
              > terminate called without an active exception
              >Am I trying to do something really illegal?
              You aren't doing something daft like calling a virtual method from a
              constructor are you?
              >
              Hi all,
              >
              by implementing the example, I think I found my mistake.
              >
              So, what was the mistake?
              Thank you guys for all the very useful suggestions!
              Of course in the end is turned out to be my stupidity:
              Is said I did

              1) upperShell.shap e = this->shape->copy();
              2) lowerShell.shap e = this->shape->copy();

              what I did not say is that I deleted the obeject which these two
              pointers were pointing two:
              delete upperShell.shap e; //I guess, this you really
              have to do
              1) upperShell.shap e = this->shape->copy();
              delete upperShell.shap e; //whereas this was a
              mistake, uppershell now should have been
              // lowershell.
              2) lowerShell.shap e = this->shape->copy();


              Thanks again everybody!

              Maximilian

              Comment

              • tragomaskhalos

                #8
                Re: pure virtual method

                On 27 Aug, 08:48, mfabric...@gmai l.com wrote:
                >
                class Shape
                {
                ...
                virtual Shape * copy() = 0;
                ...
                };
                >
                class Circle : public Shape
                {
                ...
                Shape * copy();
                };
                >
                Not directly related to your question,
                but in case you didn't know, you could
                declare Circle's copy as:
                Circle* copy();
                This can often eliminate casting from the
                calling code. (google for "covariant return
                type").

                Comment

                • anon

                  #9
                  Re: pure virtual method

                  mfabricius@gmai l.com wrote:
                  On Aug 27, 4:02 pm, anon <a...@no.nowrot e:
                  >mfabric...@gma il.com wrote:
                  >>On 27 Aug., 10:21, Ian Collins <ian-n...@hotmail.co mwrote:
                  >>>mfabric...@g mail.com wrote:
                  >So, what was the mistake?
                  >
                  Thank you guys for all the very useful suggestions!
                  Of course in the end is turned out to be my stupidity:
                  Is said I did
                  >
                  1) upperShell.shap e = this->shape->copy();
                  2) lowerShell.shap e = this->shape->copy();
                  >
                  what I did not say is that I deleted the obeject which these two
                  pointers were pointing two:
                  delete upperShell.shap e; //I guess, this you really
                  have to do
                  1) upperShell.shap e = this->shape->copy();
                  delete upperShell.shap e; //whereas this was a
                  mistake, uppershell now should have been
                  // lowershell.
                  2) lowerShell.shap e = this->shape->copy();
                  >
                  Thats why you need to post minimal compilable source that represents the
                  problem. This is not even shown on your starting post.
                  Doing so, you might even find a cause of the problem yourself.

                  Comment

                  Working...