Boost.Python call by reference : 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:)
问题描述
我正在尝试使用 Boost.Python 将我的 C++ 类公开给 Python.这是我正在尝试做的事情的简化版本:
I'm trying to expose my C++ Classes to Python using Boost.Python. Here is a simplyfied version of what i'm trying to do:
我有一个从 boost::noncopyable 派生的类 A 和第二个类 B,其方法将 A 的引用作为参数.
I have a class A deriving from boost::noncopyable and a second class B with a method that takes a reference to A as an argument.
我公开的类如下:
我在 python 中扩展 B 是这样的:
I extend B in python like this:
test.py:
并像这样调用扩展的 B:
and call the extended B like this:
但是这会导致错误消息:
However this results in an error message:
如果 A
不是从 boost::noncopyable
派生的,则代码运行没有任何错误,但是 do_something(A& a)
中的参数 acode> 在函数调用期间被复制,即使它是通过引用传入的.但是仅仅删除 A
上的不可复制要求不是一个选项,因为它存在是有原因的.
If A
isn't derived from boost::noncopyable
the code runs without any errors but the argument a in do_something(A& a)
gets copied during the function call even though it's passed in by reference. But just removing the noncopyable requirement on A
isn't an option since it's there for a reason.
有什么解决问题的建议吗?
Any suggestions how to solve the problem?
谢谢.
推荐答案
将 B.do_something(a);
更改为 B.do_something(boost::ref(a));.
参见 调用 Python 函数和方法 在 boost 手册中.
See Calling Python Functions and Methods in the boost manual.
这篇关于Boost.Python call by reference:TypeError: No to_python (by-value) converter found for C++ type:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!