gpt4 book ai didi

python - Boost.Python.ArgumentError:World.set(World, str) 中的 Python 参数类型与 C++ 签名不匹配:set(World {lvalue}, std::string)

转载 作者:行者123 更新时间:2023-11-30 05:00:41 30 4
gpt4 key购买 nike

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)
;
}

Python 终端:

>>> import hello
>>> planet = hello.World()
>>> planet.set('你好')

错误:

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)
;
}

关于python - Boost.Python.ArgumentError:World.set(World, str) 中的 Python 参数类型与 C++ 签名不匹配:set(World {lvalue}, std::string),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50647396/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com