Pure virtual function call
Asked Answered
S

2

6

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?

Sweltering answered 16/4, 2011 at 17:30 Comment(5)
The Python code you posted here contains a syntax error.Ium
The C++ class has getPosition (lower case g) and the Python class has GetPosition (upper case G). Don't know anything about Python, but maybe that does matter?Froissart
Why is getPosition() missing the virtual keyword?Disembarrass
@xeo nope @karl-von-moor just a typing error. The real code is much bigger so I just post some pseudocode.Sweltering
Oh, ok. :) And double notification with @name doesn't work in comments. :(Froissart
D
4

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

Distil answered 16/4, 2011 at 17:35 Comment(1)
I din't understand how it was solved ? There is no wrapperFraxinella
F
1

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.

Forgiveness answered 16/4, 2011 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.