0

I'm having a problem with inheriting from a python class that I generated using boost::python.

I have a class whose function Print() is defined like this:

void CMagnet::Print()
{
  cout << "Hello" << endl;
}

and the interface is defined like this in my .cpp file:

BOOST_PYTHON_MODULE (CMagnet)
{
   class_<CMagnet>("CMagnet")
      .def("Print", &CMagnet::Print)
      ;
}

In principle, the module works, but I run into problems with inheritance. Here's an example:

from CMagnet import CMagnet

class DerMagnet(CMagnet):
    def __init__(self):
        self.Print()

a = CMagnet()
a.Print()

b = DerMagnet()

What I get is:

hirbel> python der_test.py

Hello

Traceback (most recent call last):
  File "der_test.py", line 10, in <module>
    b = DerMagnet()
  File "der_test.py", line 5, in __init__
    self.Print()
Boost.Python.ArgumentError: Python argument types in
    CMagnet.Print(DerMagnet)
did not match C++ signature:
    Print(CMagnet {lvalue})

Meaning that when I instantiate the CMagnet class, I can call the Print() method without problems, but when I inherit from it and the derived class tries to call the method, the self argument is automatically inserted as the first argument and the signature is wrong. How would I resolve this?

Thanks a lot for your help.

zeus300
  • 1,017
  • 2
  • 12
  • 30

1 Answers1

1

I confirm @kerim comment.

If you don't call parent class "__init__" method, you got this kind of error message.

MatthieuW
  • 2,292
  • 15
  • 25