Boost Python - 用参数包装构造函数
Boost Python - Wrapping constructor with arguments
我已经设计了一个cpp
共享库,现在我想做一个Python
包装器来使用它。一切正常,直到有必要更改在其上添加一个参数的cpp
库构造函数。
我想知道如何在包装器中反映此参数,因为下面的代码不再起作用。我对代码进行了一些更改,现在它就像波纹管一样。我几乎可以肯定问题出在这一行
py::class_<Wrapper>("Wrapper", py::init<>())
但我不知道如何在这里添加参数。我试过了
py::class_<Wrapper>("Wrapper", py::init<>(const std::string ¶m))
还有
py::class_<Wrapper>("Wrapper", py::init<const std::string ¶m>())
但两者都失败了。
编辑一些评论后,我决定使用(没有参考(
py::class_<Wrapper>("Wrapper", py::init<const std::string param>())
但我仍然有相同的错误消息。
包装器.hpp
#include "mycpplib.hpp"
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
#include <boost/python/dict.hpp>
namespace py = boost::python;
namespace np = boost::python::numpy;
class Wrapper
{
public:
// change: inclusion of the new parameter
Wrapper(const std::string ¶m);
py::dict function1();
};
包装纸.cpp
#include "wrapper.hpp"
namespace py = boost::python;
namespace np = boost::python::numpy;
// change: inclusion of the new parameter
Wrapper::Wrapper(
const std::string ¶m) {
//do something
}
py::dict
Wrapper::function1() {
//do something
}
BOOST_PYTHON_MODULE(libwrapper)
{
Py_Initialize();
np::initialize();
py::class_<Wrapper>("Wrapper", py::init<const std::string param1>())
.def("_function1", &Wrapper::function1)
;
}
wrapper.py
import libwrapper
class Wrapper(libwrapper.Wrapper):
# change: inclusion of the new parameter
def __init__(self, param):
libwrapper.Wrapper.__init__(self, param)
def function1(self):
return self._function1()
错误是:
/path/wrapper.cpp: In function 'void init_module_libwrapper()':
/path/wrapper.cpp:24:69: error: template argument 1 is invalid
py::class_<Wrapper>("Wrapper", py::init<const std::string param1>())
^
重新整理提升文档 (https://www.boost.org/doc/libs/1_68_0/libs/python/doc/html/tutorial/tutorial/exposing.html( 我发现:
py::class_<Wrapper>("Wrapper", py::init<const std::string param1>())
应该这样写:
py::class_<Wrapper>("Wrapper", py::init<const std::string>())
没有参数名称。只是类型
相关文章:
- 线程构造函数周围的可变参数模板包装器无法编译
- 具有值包装器的可变参数模板构造函数的类构造函数优先级
- 无法理解包装类的构造函数的声明
- Boost Python - 用参数包装构造函数
- 标准库中是否有与 std::thread 的构造函数语义匹配的类型擦除函数包装器?
- 重构现有包装类,以使用与包装器本身相同的构造函数参数包装 T
- 如何为包装容器的模板类编写构造函数,其中容器可以是数组或向量
- SWIG - 如何使用采用'const double*'的构造函数将类包装为 python
- 将 C 结构工厂函数与其相应的 C++ 包装类构造函数合并
- 将C 模板构造函数包装在模板类中
- 如何声明包装器类型 X 的移动构造函数<T> noexcept 取决于is_nothrow_move_constructible<T>?
- 通过包装器对象的构造函数将依赖关系传递给包装器对象
- 如何使包装类将其构造函数参数转发到 std::vector 的构造函数?
- 通过c++中的包装器将arment列表传递给构造函数
- boost-python:如何提供自定义构造函数包装器函数
- std::vector包装器的构造函数
- SWIG 3使用模板化构造函数包装未模板化的类
- 用std::类似向量的构造函数对C数组进行Constexpr包装
- 如何在SWIG包装C++代码中向目标语言(特别是Python)添加替代构造函数
- 当<T> T 没有复制构造函数时,std::queue 的虚拟包装器不会编译