[C++-sig] Boost.Python and abstract class...

Baptiste Lepilleur gaiacrtn at free.fr
Thu Apr 3 10:48:05 CEST 2003


    Hi all,
    I'm a newbie to Boost.Python and I'm trying to expose an abstract class
to python, as demonstrated in "Class Virtual Functions" of the tutorials
(http://boost.org/libs/python/doc/tutorial/doc/class_virtual_functions.html)
.

    If I try the code provided in the tutorial, I get the following error:

>>> class Derived(Base):
...   def f(self):
...     return 42
...
>>> d = Derived()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
RuntimeError: This class cannot be instantiated from Python

    I fixed this by adding a constructor:

>>> class Derived(Base):
...   def __init__(self):
...     pass
...   def f(self):
...     return 42
...
>>> d = Derived()
>>> call_f(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: bad argument type for built-in operation

    But then I get the above error. If I make the Base class non abstract
and expose it without boost::noncopyable and no_init, then call_f works
correctly.

   Works (with making Base non abstract):
   class_<Base, BaseWrap>("Base");

    Don't work (see above):
    class_<Base, BaseWrap, boost::noncopyable>("Base");

    I'm using VC6 SP5 and Boost 1.30.

    Anyone knows what is wrong ?

    Baptiste.






More information about the Cplusplus-sig mailing list