C++ 错误:调用了纯虚方法 - 在没有活动异常的情况下终止调用 - 中止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5407304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: pure virtual method called - terminate called without an active exception - Aborted
提问by Xitrum
In my A.h file:
在我的 Ah 文件中:
class A{
private:
unsigned short PC;
public:
A():PC(0){}
virtual ~A(){}
virtual void execute(unsigned short PC)=0;
};
In my B.h file:
在我的 Bh 文件中:
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:
在我的 B.cpp 文件中:
#include <iostream>
#include "B.h"
void B::execute (unsigned short PC){
cout << "Run";
}
In my Functions.h file :
在我的 Functions.h 文件中:
#include "A.h"
class Functions{
public:
int status;
Functions():status(1){} // this is a constructer
void run(A *a);
};
IN my Functions.cpp file:
在我的 Functions.cpp 文件中:
#include "Functions.h"
#include "A.h"
#include "B.h"
using namespace std;
void Functions::run (A *a){
a->execute();
}
In my Main.cpp file:
在我的 Main.cpp 文件中:
#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 - AbortedCan anybody where did I misunderstand? Thank you
当我运行时,它有一些错误: 调用的纯虚方法 - 终止调用而没有活动异常 - 中止任何人都误解了我的意思吗?谢谢
回答by Alexander Poluektov
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).
(我假设您的演示代码不完整)。
回答by Marius Bancila
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;
}
回答by Adrian
You wrote B() constructor without paranthesis , plus you have 2 B() default constructors, plus one of them is virtual(not good), plus when you execute a->execute, again you need paranthesis with an argument, because it is a function:
你写了没有括号的 B() 构造函数,加上你有 2 个 B() 默认构造函数,加上其中一个是虚拟的(不好),加上当你执行 a->execute 时,你又需要带参数的括号,因为它是一个功能:
Try this:
尝试这个:
#include <iostream>
using namespace std;
class A{
private:
unsigned short PC;
public:
A():PC(0){}
virtual ~A(){}
virtual void execute(unsigned short PC)=0;
};
class B:public A{
private:
int status;
bool exe;
public:
B():status(0),exe(false){}
//virtual B(){}
void execute (unsigned short PC);
};
void B::execute (unsigned short PC){
cout << "Run";
}
class Functions{
public: int status;
Functions():status(1){} // this is a constructer
void run(A *a);
};
void Functions::run (A *a){
a->execute(1);
}
int main(int args, char**argv){
A *a;
B b;
a = &b;
Functions f;
f.run(a);
return 1;
}
回答by fanni
int main(int args, char**argv){
A *a;
B b; // wrong
a = &b; // wrong
Functions f;
f.run(a);
return 1;
}
you can do this :
你可以这样做 :
int main(int args, char**argv){
A *a = new B;
//B b;
//a = &b;
Functions f;
f.run(a);
return 1;
}