开发者

Error: pure virtual method called [closed]

It's difficult to tell what is being asked here. This question is amb开发者_运维知识库iguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 9 years ago.

In my AC.h file:

#ifndef _AC_H
#define _AC_H
class AC{
private:
    unsigned short PC;
public:
    AC():PC(0){}
    virtual ~AC(){}
    virtual void test()=0;
};
#endif

In my R500.h file:

#ifndef _R500_H
#define _R500_H
#include <iostream>
#include "AC.h"
class R500:public AC{
private: int mCStatus;
public:
    R500():mCStatus(0){}
    virtual ~R500(){}
    void test();
};
#endif

In my R500.cpp

#include <iostream>
#include "R500.h"
using namespace std;
void R500::test(){
    cout << "test called\n";
}

In my AF.h file

#ifndef _AF_H
#define _AF_H
#include "AC.h"
class AF{
public: int status;
    AC *ac;
    AF():status(1){} // this is a constructer
    void menu();
    void run();
};
#endif

In my AF.cpp file:

#include <iostream>
#include "R500.h"
#include "AF.h"
#include "AC.h"

using namespace std;
void AF::menu() {
    R500 r;
    ac = &r;
    run();
}

void AF::run() {
    ac->test();
}

In my Main.cpp

#include <iostream>
#include "AF.h"
#include "R500.h"
#include "AC.h"

using namespace std;

int main(int args, char**argv){
    AF af;
    af.menu();
    return 0;
}

It compiled well but when I run, it said that

pure virtual method called

terminate called without an active exception

Aborted

Can anybody tell me where were I wrong? Thank you.


</Telepathy mode:on>

Somewhere in your code (which you didn't posted apparently) you've called virtual function from constructor or destructor. This function have happened to be pure virtual in class which constructor/destructor you called it from.

You've got pure virtual call because at that point of time, full sub-object was either not fully constructed yet, or already destroyed. And with full sub-object was destroyed its virtual function table. So you was left with virtual function table from your abstract class, and via this table you've called pure virtual function.

Check also this link: When my base class's constructor calls a virtual function on its this object, why doesn't my derived class's override of that virtual function get invoked?


There is several things that can be improved in your code.

But for your question, this code compiles and runs fine on Visual Studio 2008 :

#include <iostream>
using namespace std;

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

class R500 : public AC
{
private: 
    int mCStatus;
public:
    R500(): mCStatus(0) {}
    virtual ~R500() {}
    void test();
};

void R500::test()
{
    cout << "test called\n";
}

class AF
{
public: 
    int status;
    AC *ac;
    AF() : status(1) {} // You forgot to add a body to your constructor here "{}"
    void menu();
    void run();
};

void AF::menu() 
{
    R500 r;
    ac = &r;
    run();
}

void AF::run() 
{
    ac->test();
}

int main(int args, char** argv)
{
    AF af;
    af.menu();
    return 0;
}

The only two things that I did are :

  • Add a body to the constructor of AF
  • Remove the global variable ac


You have two instances of AC *ac; One in AF class defined in AF.h and another one with global scope in AF.cpp. The code you posted is not exactly the one leading to the issue so it's hard to tell you more.


Background: When constructing a C++ object, you first construct the base-class, then each intermediate class in turn, finishing with the concrete class. While each layer is constructed, it only has access to virtual functions that itself, or superclasses, have defined. The same mechanism take place when the object is destroyed, albeit reversed.

If the a constructor/destructor calls a virtual function that only is defined by a subclass, you will trigger the pure virtual function called error, which will immediate abort your program.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜