纯虚函数调用
我正在使用 boost.python 来制作用 C++ 编写的 python 模块。我有一些带有纯虚函数的基类,我像这样导出:
class Base
{
virtual int getPosition() = 0;
};
boost::python::class_<Base>("Base")
.def("GetPosition", boost::python::pure_virtual(&Base::getPosition));
在Python中我有代码:
class Test(Base):
def GetPosition(self):
return 404
Test obj
obj.GetPosition()
运行时错误:调用纯虚函数
什么问题?
I'm using boost.python to make python-modules written in c++. I have some base class with pure virtual functions which I have exported like this:
class Base
{
virtual int getPosition() = 0;
};
boost::python::class_<Base>("Base")
.def("GetPosition", boost::python::pure_virtual(&Base::getPosition));
in Python I have code:
class Test(Base):
def GetPosition(self):
return 404
Test obj
obj.GetPosition()
RuntimeError: Pure virtual function called
What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当构造函数或析构函数直接或间接调用纯虚拟成员时,会发生此错误。
(请记住,在构造函数和析构函数执行期间,动态类型是构造/析构类型,因此虚拟成员将解析为该类型)。
This error happens when a constructor or a destructor directly or indirectly calls a pure virtual member.
(Remember that during constructor and destructor execution, the dynamic type is the constructed/destructed type and so virtual members are resolved for that type).
“纯虚函数”是在基类中没有定义的函数。这意味着该基类的所有子级都将具有该函数的重写实现,但基类没有实现。
在您的示例中,看起来您正在调用纯虚函数,因此您正在调用已声明的函数,但由于您没有调用任何子级的实现,因此它没有定义。
A "pure virtual function" is a function that has no definition in the base class. It means that all children of that base class will have an overridden implementation of that function, but the base class does not have an implementation.
In your example, it looks like you are calling a pure virtual function, so you are calling a function that is declared, but since you are not calling any child's implementation, it has no definition.