Boost.Python.ArgumentError: World.set(World, str) 中的 Python

Boost.Python.ArgumentError: Python argument types in World.set(World, str) did not match C++ signature: set(World {lvalue}, std::string)

本文关键字:World Python str 中的 set ArgumentError Boost      更新时间:2023-10-16
I am learning boost-python from the Tutorial, 

但是遇到错误,你能给我一些提示吗,谢谢!

#include <boost/python.hpp>
using namespace boost::python;
struct World
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};

BOOST_PYTHON_MODULE(hello)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}

蟒蛇终端:


>>> import hello>>> planet = hello.World()
>>> planet.set('howdy')

错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
World.set(World, str) did not match C++ signature:
set(World {lvalue}, std::string)

使用来自 [Boost Python 的演示代码 教程][1][1]
: https://www.boost.org/doc/libs/1_51_0/libs/python/doc/tutorial/doc/html/python/exposing.html

将程序更改为代码,如下所示,它将起作用

#include <boost/python.hpp>
using namespace boost::python;
struct World
{   
World(){}; // not mandatory
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
BOOST_PYTHON_MODULE(hello)
{
class_<World>("World", init<>())  /* by this line
your are giving access to python side 
to call the constructor of c++ structure World */
.def("greet", &World::greet)
.def("set", &World::set)
;
}