【发布时间】:2017-11-14 22:27:20
【问题描述】:
我想了解为什么以下函数在python中不起作用:
#include<boost/python.hpp>
#include<iostream>
#include<string>
void hello(std::string& s) {
std::cout << s << std::endl;
}
BOOST_PYTHON_MODULE(test)
{
boost::python::def("hello", hello);
}
当我将库导入 python 时
import test
test.hello('John')
我得到一个错误:
test.hello(str)
did not match C++ signature:
hello(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > {lvalue})
一切都只适用于'std::string s',但我想通过引用而不复制它来引用对象。我注意到任何其他带有引用的函数都会弹出错误,例如整数&。
【问题讨论】:
-
如果一种方式有效,而另一种方式给你一个错误信息,那通常意味着你应该以第一种方式做事。
-
字符串来自 Python。如果你得到它的引用,你可以修改它。但是字符串在 Python 中是不可变的。所以你不能这样做。
标签: python c++ boost-python