【问题标题】:Having trouble matching up argument types between C++ and Boost::Python在 C++ 和 Boost::Python 之间匹配参数类型时遇到问题
【发布时间】:2017-03-28 15:12:23
【问题描述】:

我正在为一个 API 创建一个 C++ 到 python 的接口,我不允许修改 C++ 的重要部分来获取我的接口。

我选择了 Boost::Python 作为我从 C++ 到 python 的桥梁。我已经定义了 BOOST_PYTHON_MODULE 的以下部分:

我的 C++ 部分的开始:

using namespace boost::python;

BOOST_PYTHON_MODULE(sandman_lib) {

    class_<manblock::ManDriver>("ManDriver", init< ... >())

    scope sandprogression  = class_<SandProgression>("sandprogression");
    def("api_create_sand_c_side_from_cfg_file", api_create_sand_c_side_from_cfg_file);
}

从我的解释器来看,我对这个接口的使用如下:

>>> import sandman_lib
>>> SAND = sandman_lib.sand_progression()
>>> SAND
<sandman_lib.sandprogression object at 0x7ffff7eaa998>
>>> SAND.api_create_sand_c_side_from_cfg_file("path_to_my_config_file")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
    sandprogression.api_create_sand_c_side_from_cfg_file(sandprogression, str)
did not match C++ signature:
    api_create_sand_c_side_from_cfg_file(char const*)

当然,在“sandprogression”范围内,我们有我们上面尝试使用的这个函数的原型,它被定义为:

extern "C" void api_create_sand_c_side_from_cfg_file(const char* cfg_file_name);

我会假设某些类型可以在 C++ 和 Python 之间自动转换,但显然情况并非如此。我一直在翻阅 boost 文档以尝试了解如何简单地显式设置映射,但我似乎无法掌握它是如何完成的。

【问题讨论】:

    标签: python c++ boost boost-python


    【解决方案1】:

    应该使用sandman_lib.sandprogression.api_blah("path") 而不是sandman_lib.sandprogression().api_blah("path")。看example on boost.org

    【讨论】:

    • 谢谢。在我再次编译之前我没有注意到这个错误。你的后一个建议在那里解决了问题,但我仍然得到 ArgumentError,其中函数调用不会自动从 str 转换为 const char*。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    相关资源
    最近更新 更多