【发布时间】:2017-09-07 01:50:49
【问题描述】:
我有一个类Foo 和一个成员x 我想公开,但通过getter 函数而不是属性。我刚刚发现了make_getter,所以我想试试看:
#include <boost/python.hpp>
namespace py = boost::python;
struct Base {
int x;
};
struct Foo : Base {
Foo(int i): Base{i} { }
};
BOOST_PYTHON_MODULE(Foo)
{
py::class_<Foo>("Foo", py::init<int>())
.def_readonly("x", &Foo::x)
.def("getX", py::make_getter(&Foo::x))
;
}
然而,这失败了:
>>> import Foo
>>> f = Foo.Foo(42)
>>> f.x
42
>>> f.getX()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
Foo.getX(Foo)
did not match C++ signature:
getX(Base {lvalue})
>>>
这个错误是什么意思?显然签名匹配!我怎样才能解决这个问题?
【问题讨论】:
标签: c++ boost-python