Error: pure virtual method called - terminate called without an active exception - Aborted

c++
34,947

Solution 1

Usually you get this error when call your virtual function from constructor or destructor. Check that that is not the case.

(I assume that your demo code is not complete).

Solution 2

Don't know what you're doing but this, which is basically what you're showing us, except that is simplified and actually compiling, runs without any problems:

#include <iostream>
class A
{
public:
   virtual ~A() {}
   virtual void execute() = 0;
};
class B: public A
{
public:
   virtual void execute() {std::cout << "B::execute" << std::endl;}
};
void execute(A* a)
{
   a->execute();
}
int main()
{
   A* a;
   B b;
   a = &b;
   execute(a);
   return 0;
}
Share:
34,947
Xitrum
Author by

Xitrum

merge me

Updated on March 12, 2020

Comments

  • Xitrum
    Xitrum almost 3 years

    In my A.h file:

    class A{
      private:
        unsigned short PC;
      public:
        A():PC(0){}
        virtual ~A(){}
        virtual void execute(unsigned short PC)=0;
    };
    

    In my B.h file:

    class B:public A{
      private: 
          int status;bool exe;
      public:
        B:status(0),exe(false){}
        virtual B(){}
        void execute (unsigned short PC);
    };
    

    In my B.cpp file:

    #include <iostream>
    #include "B.h"
    void B::execute (unsigned short PC){
      cout << "Run";
    }
    

    In my Functions.h file :

    #include "A.h"
    class Functions{
      public: 
        int status;
        Functions():status(1){} // this is a constructer
        void run(A *a);
    };
    

    IN my Functions.cpp file:

    #include "Functions.h"
    #include "A.h"
    #include "B.h"
    using namespace std;
    void Functions::run (A *a){
      a->execute();
    }
    

    In my Main.cpp file:

    #include "A.h" 
    #include "B.h" 
    int main(int args, char**argv){
      A *a;
      B b;
      a = &b;
      Functions f;
      f.run(a);
      return 1;
    }
    

    When I run , it has some error: pure virtual method called - terminate called without an active exception - Aborted Can anybody where did I misunderstand? Thank you

    • Omnifarious
      Omnifarious almost 12 years
      That code has numerous errors that prevent it from compiling. That means it isn't the code that's causing your problem, and instead an attempt to mock up something resembling the code that's causing your problem. And that means the mock up (even if all the errors were fixed) might not cause your problem. In fact, it doesn't look like it would cause the problem you're seeing. If you're going to post mock-up code, test that code first to make sure it causes the same problem as the original code.
    • Marius Bancila
      Marius Bancila almost 12 years
      to get good answers please post actual code or snippets that make sense
    • Martin York
      Martin York almost 12 years
      @Marius Bancila: Posting snippets for a runtime problem is only useful in the minority of cases. Actual compilable code that demonstrates the problem is what is really needed.
    • Marius Bancila
      Marius Bancila almost 12 years
      sure, won't argue with that. but even snippet are actually better than code with lots of obvious errors
  • Adrian
    Adrian almost 12 years
    I execute on my comp, and i didnt get errors, it prints: "Run".
  • Adrian
    Adrian almost 12 years
    @user552279: it should work, if you copy paste the example, the code is good
  • Xitrum
    Xitrum almost 12 years
    void Functions::run (A *a){ a->execute(1); } 1?
  • Adrian
    Adrian almost 12 years
    well you are asking for an usigned short, so i just put 1 to compile
  • Xitrum
    Xitrum almost 12 years
    ah, sorry, i still don't know why it doesn't work on my computer. Everything is the same.
  • Xitrum
    Xitrum almost 12 years
    pure virtual method called terminate called without an active exception Aborted Same error as the beginning :(
  • Adrian
    Adrian almost 12 years
    did you copy paste the code or did you fix all the errors that i told you ?
  • Adrian
    Adrian almost 12 years
    copy paste the code i gave you, and tell me then if you still get the same error
  • Omnifarious
    Omnifarious almost 12 years
    @user552279: What platform and compiler are you using?
  • Alexander Malakhov
    Alexander Malakhov about 10 years
    Or you call method while object is destructing. Read here: tombarta.wordpress.com/2008/07/10/…
  • ady
    ady over 6 years
    Thank you, I called a function from constructor and then the function called a virtual function. Okay, this is a problem because you still can be in the process of creating an object for a base class while you want to call a virtual function which should be called for the non-existing object of a derived class.