Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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

like image 675
Xitrum Avatar asked Mar 23 '11 15:03

Xitrum


2 Answers

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).

like image 169
Alexander Poluektov Avatar answered Sep 18 '22 18:09

Alexander Poluektov


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;
}
like image 20
Marius Bancila Avatar answered Sep 18 '22 18:09

Marius Bancila