我正在尝试从boost python教程中运行该示例,而我遇到的问题与本帖子Boost.Python.ArgumentError: Python argument types in World.set(World,str) did not match C++ signature: set(World {lvalue},std::string)中的问题完全相同。对于重复的内容,我深表歉意,但由于声誉低下而无法发表评论。
但是我遵守了前面提到的帖子的公认答案,所以我的课程看起来像
struct World_std
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
并通过
暴露给python#include <boost/python.hpp>
#include <world_std.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(hello_std)
{
class_<World_std>("World_std",init<>())
.def("greet",&World_std::greet)
.def("set",&World_std::set)
;
}
python终端的输出是
>>> import hello_std
>>> planet = hello_std.World_std()
>>> planet.set('howdy')
Traceback (most recent call last):
File "<stdin>",line 1,in <module>
Boost.Python.ArgumentError: Python argument types in
World_std.set(World_std,str)
did not match C++ signature:
set(World_std {lvalue},std::string)
涉及的软件/库版本:
CentOS Linux 7 (Core)
Boost version: 1.71.0
Python version: 3.7.3
Compiler: gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39)
Makefile generation: cmake version 3.14.6
Make: GNU Make 3.82
当我将std::string
更改为boost::python::str
时,错误消失了。有什么方法可以实现Boost.Python负责将python的str转换为std :: string吗?
感谢您的帮助。先感谢您。