Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...