Object of self type in class - TypeError: No to_python (by-value) converter found for C++ type
Object of self type in class - TypeError: No to_python (by-value) converter found for C++ type
我正在尝试使用 boost.python 公开具有 2 个成员(val 和父级)的 class(子级)。两者都是 public 并且 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 位)。
以下是不同的代码片段:
Child.h
class Child {
public:
Child();
double val;
Child* parent;
};
Child.cpp
#include "Child.h"
Child::Child()
{
val = 0.0;
}
Wrapper.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)
;
}
Wrapper.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 的 public 成员,但我不确定这是访问它的正确方法。
我试图在 boost.python 文档中查找信息并查看了
和
Boost.Python call by reference : TypeError: No to_python (by-value) converter found for C++ type:
但是如果我的问题的解决方案在那里,我没有得到它。
无论如何,如果有人能帮我纠正我的错误并解释我为什么错了,我将不胜感激!
谢谢!
编辑
我想通了,替换行
boost::python::class_<Child>("Child")
来自
boost::python::class_<Child, Child*>("Child")
在 Wrapper.cpp 中解决了我的问题。结果如预期:
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
0.0
C:\Users\Visual Studio 2017\Projects\MinExBoostPython2\x64\Debug>
希望对大家有所帮助!
我正在尝试使用 boost.python 公开具有 2 个成员(val 和父级)的 class(子级)。两者都是 public 并且 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 位)。
以下是不同的代码片段:
Child.h
class Child {
public:
Child();
double val;
Child* parent;
};
Child.cpp
#include "Child.h"
Child::Child()
{
val = 0.0;
}
Wrapper.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)
;
}
Wrapper.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 的 public 成员,但我不确定这是访问它的正确方法。
我试图在 boost.python 文档中查找信息并查看了
和
Boost.Python call by reference : TypeError: No to_python (by-value) converter found for C++ type:
但是如果我的问题的解决方案在那里,我没有得到它。
无论如何,如果有人能帮我纠正我的错误并解释我为什么错了,我将不胜感激!
谢谢!
编辑
我想通了,替换行
boost::python::class_<Child>("Child")
来自
boost::python::class_<Child, Child*>("Child")
在 Wrapper.cpp 中解决了我的问题。结果如预期:
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
0.0
C:\Users\Visual Studio 2017\Projects\MinExBoostPython2\x64\Debug>
希望对大家有所帮助!