使用c ++中的Base类在python中创建Derived类

时间:2016-10-12 14:24:47

标签: python c++ boost-python

我有一个班级" Base"它的派生" MdBase" 我还有一个类Factorybase 由于两个类中都有virtual_pure函数,我必须定义MDBaseWrapper和FactoryBaseWrapper。

BOOST_PYTHON_MODULE ( modulepython )
{
  class_<MySpace::FactoryBase, boost::noncopyable>( "FactoryBaseCpp", no_init );
  class_<MySpace::Base, boost::noncopyable>( "Base", no_init );
  class_<MySpace::MdBase, boost::noncopyable>( "MdBaseCpp", no_init );
  class_<MySpace::Key, boost::noncopyable>( "Key", no_init )
  .add_property( "sourceTradable",
                 make_getter( &MySpace::Key::sourceTradable, return_internal_reference<>()));
  class_<Interface::Tradable, boost::noncopyable>( "Tradable", no_init );

  class_<FactoryBaseWrapper, boost::noncopyable>( "FactoryBase", no_init )
  .def( init<const std::string&, const MySpace::FactoryConfig&>())
  .def( init<const std::string&, MySpace::FactoryMgr&, const std::string&, bool>())
  .def( "getName", &FactoryBaseWrapper::getName, return_internal_reference<>())
  .def( "create", &FactoryBaseWrapper::create, return_internal_reference<>());

  class_<MDBaseWrapper, boost::noncopyable>( "MdBase", init<MDBaseWrapper>())
  .def( init<MySpace::FactoryBase&, const MySpace::Key&,
  const Interface::Tradable&, int, int>())  
}

一个简单的python脚本。 工厂只返回一个简单的MdBase Derived类。

 import modulePython as pyMod 

class DerivedTest(pyMod.MdBase):
    def __init__(self, factory, key, tradable, exchangeType, changeMask):
        super(DerivedTest, self).__init__(factory, key, tradable, exchangeType, changeMask)
        print "Test"


class PythonFactory(pyMod.FactoryBase):
    def __init__(self):
        print "Creating Factory"

    def create(self, key):
        print "Creating Test"
        return DerivedTest(self, key, key.sourceTradable, 0, 0)

当我打电话给#34;创建&#34;来自cpp我得到了他的错误。

Traceback (most recent call last):
  File "PyFactory.py", line 21, in create
    return DerivedTest(self, key, key.sourceTradable, 0, 0)
  File "PyFactory.py", line 8, in __init__
    super(DerivedTest, self).__init__(factory, key, tradable, Type, changeMask)
Boost.Python.ArgumentError: Python argument types in
    MdBase.__init__(DerivedTest, PythonFactory, Key, Tradable, int, int)
did not match C++ signature:
    __init__(_object*, MySpace::FactoryBase {lvalue}, MySpace::Key, Interface::Tradable, int, int)
    __init__(_object*, MDBaseWrapper)
line 51:  4306 Segmentation fault  (core dumped)

所以我认为python可能没有获得类之间的继承,并且像这样改变它以表明包装器继承自FactoryBase。

 class_<FactoryBaseWrapper, bases<MySpace::FactoryBase>, boost::noncopyable>( "FactoryBase", no_init )

现在,我收到了该行的编译错误。

boost_1_55_0/boost/python/object/class_metadata.hpp:57: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::mpl::not_<boost::is_same<MySpace::FactoryBase, MySpace::FactoryBase> >::************)’

你看到代码中出了什么问题吗? 谢谢

0 个答案:

没有答案