在声明外部构造函数时,不能在boost :: python中使用make_constructor

问题描述 投票:0回答:1

我正在尝试在将类移植到python时定义外部构造函数,使用make_constructor绝对失败。当我尝试:

#include <boost/python/numpy.hpp>
using boost::python;

class foo
{
    int i;
public:
    foo(int i) : i(i){}
};

foo foo_create(int i){return foo(i);}

BOOST_PYTHON_MODULE(bar)
{
    class_<foo>("foo")
        .def("__init__", make_constructor(&foo_create));
}

我收到以下错误

error: no type named ‘element_type’ in ‘class foo’

我尝试使用noinit和init()具有相同的结果。我究竟做错了什么?

boost-python
1个回答
0
投票

敬畏发现了这个问题,其中一部分是关于make_construction的非常稀疏的文档。我需要将ptr返回到一个像这样的新实例(在这种情况下,我使它们成为共享指针):

#include <boost/python/numpy.hpp>
#include <memory>
using boost::python;

class foo
{
    int i;
public:
    foo(int i) : i(i){}
};

std::shared_ptr<foo> foo_create(int i){return std::shared_ptr<foo>(foo(i));}

BOOST_PYTHON_MODULE(bar)
{
    class_<foo, std::shared_ptr<foo>>("foo")
        .def("__init__", make_constructor(&foo_create));
}

关于make_constructor的文档真的很稀疏,但这里有一些讨论:https://wiki.python.org/moin/boost.python/HowTo在点“9”下。

© www.soinside.com 2019 - 2024. All rights reserved.