【发布时间】:2019-03-30 14:23:17
【问题描述】:
我正在尝试使用 boost.python 公开一个具有 2 个成员(val 和 parent)的类(Child)。两者都是公共的,并且父级是子类型。编译没有错误,但是当我尝试访问父级时,我的 python 包装器带来了以下内容:
TypeError: No to_python (by-value) converter found for C++ type: class
我对 C++、python 和 boost.python 还是很陌生。我可能遗漏了一些非常明显的东西,但我不太明白这里有什么问题。
我在 Windows 7 上使用 Visual Studio Community 2017 和 python2.7(均为 64 位)。
以下是不同的代码:
孩子.h
class Child {
public:
Child();
double val;
Child* parent;
};
Child.cpp
#include "Child.h"
Child::Child()
{
val = 0.0;
}
包装器.cpp
#include <boost/python.hpp>
#include "Child.h"
BOOST_PYTHON_MODULE(WrapPyCpp)
{
boost::python::class_<Child>("Child")
.def_readwrite("val", &Child::val)
.def_readwrite("parent", &Child::parent)
;
}
包装器.py
import WrapPyCpp
class_test = WrapPyCpp.Child()
class_test.val = 1.0
class_test_parent = WrapPyCpp.Child()
class_test.parent = class_test_parent
print class_test_parent.val
print dir(class_test)
print class_test.val
print class_test.parent.val
当我启动 Wrapper.py 时,最后一行抛出异常:
C:\Users\Visual Studio 2017\Projects\MinExBoostPython2\x64\Debug>Wrapper.py
0.0
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__instance_size__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'parent', 'val']
1.0
Traceback (most recent call last):
File "C:\Users\Visual Studio 2017\Projects\MinExBoostPython2\x64\Debug\
Wrapper.py", line 12, in <module>
print class_test.parent.val
TypeError: No to_python (by-value) converter found for C++ type: class Child * __ptr64
C:\Users\Visual Studio 2017\Projects\MinExBoostPython2\x64\Debug>
我希望访问 parent 及其成员 val。 据我了解该错误,我想我如何公开声明为指针的父级存在问题。我说的对吗?
我使用了.def_readwrite("parent", &Child::parent),因为它是 Child 的公共成员,但我不确定它是否是访问它的正确方法。
我尝试在 boost.python 文档中查找信息并查看了
TypeError: No to_python (by-value) converter found for C++ type
和
Boost.Python call by reference : TypeError: No to_python (by-value) converter found for C++ type:
但如果我的问题的解决方案在那里,我没有得到它。
无论如何,如果有人能帮助我纠正我的错误并解释我为什么错了,我将非常感激!
谢谢!
【问题讨论】:
标签: c++ python-2.7 visual-studio boost-python