通过引用传递时std::vector的转换器
Converter for std::vector when passed by reference
这是std::vector to boost::python::list
我尝试了提供的示例:
// C++ code
typedef std::vector<std::string> MyList;
class MyClass {
MyList myFuncGet();
void myFuncSet(MyList& list)
{
list.push_back("Hello");
}
};
// My Wrapper code
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(mymodule)
{
class_<MyList>("MyList")
.def(vector_indexing_suite<MyList>() );
class_<myClass>("MyClass")
.def("myFuncGet", &MyClass::myFuncGet)
.def("myFuncSet", &MyClass::myFuncSet)
;
}
但当我尝试在Python中实际使用它时,我会遇到一个错误(见底部)。
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from mymoduleimport *
>>> mc = MyClass()
>>> p = []
>>> mc.myFuncSet(p)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
MyClass.myFuncSet(MyClass, list)
did not match C++ signature:
myFuncSet(MyClass {lvalue}, std::vector<std::string, std::allocator<std::string> > {lvalue})
根据我通过阅读其他网站收集到的信息;员额,需要一个转换器。有人能通过添加必要的转换器代码来完成我的示例吗?我会自己做,但我对boost不够熟悉,不知道这样的转换器是什么样子的。
提前感谢!
我相信只有在通过值或常量引用传递参数时才能使用转换器。传递非一致引用要求直接暴露类型。这意味着,如果您想在不复制列表项的情况下将列表从python传递到c++,则需要更改代码以使用boost::python::list
而不是MyList
,后者类似于(未经测试的)
void myFuncSet(boost::python::list& list)
{
list.append("Hello");
}
矢量索引套件将类似python列表的行为添加到MyList绑定中,它不允许您在其位置传递python列表。
您在示例中遇到的错误是,您试图将python列表传递给使用std::vector<int>
的函数。我怀疑这会起作用
p = mc.myFuncGet()
mc.myFuncSet(p)
这是一篇关于编写转换器的非常有用的文章。http://misspent.wordpress.com/2009/09/27/how-to-write-boost-python-converters/
相关文章:
- 转换器练习:跳过 if 语句和 if 语句
- 如何将唯一指针的 std::vector 转换为原始指针的 std::span?
- 使用转换器提升 Python 问题 - 静态链接
- 我写的 ASCII 到二进制转换器C++向后显示二进制,如何使其正确显示?
- 十进制到二进制转换器 c++
- 如何在特征中将 std::vector 转换为矩阵?
- 用于 boost::p roperty_tree 的通用枚举转换器
- 为什么我的C++基转换器中出现分段错误?
- 将字节数组转换为对象C++ |C++中的 C# 位转换器
- 英语到摩尔斯电码转换器
- C++如何将 std::vector 转换为 const<int> std::vector<const int>?
- 我用c++做了一个转换器,把英里转换成公里,把公里转换成英里,把赛尔修斯转换成法赫伦海特,把法赫伦海特转换成赛尔修斯
- 使用 Json 转换器反序列化 WCF 服务中的C++字符串
- 十进制,包括负到 32 位二进制转换器
- 标准::映射转换器模板
- 英里到千克转换器练习的小语法错误
- 句子转换器在存在新行的情况下不起作用
- 在C Windows程序中获取A/D转换器示例程序
- ROS中未定义的参考误差到OpenCV图像转换器
- 通过引用传递时std::vector的转换器