首页 > 解决方案 > boost-python python3 不会将 str 转换为 std::string

问题描述

我试图公开一个非常简单的功能:

  void f(const std::string &c) {
      std::cout << c << std::endl;
   }

    def("f", &::f);

python2 工作正常,但是当我将代码与 python3 链接时,出现此错误:

import p
p.f('aaa')
Boost.Python.ArgumentError: Python argument types in
    p.f(str)
did not match C++ signature:
    f(std::string)

我注意到 python2 中存在的 stringobj.h 不再存在于 python3 中。

如果我用 f(char const*c) 替换 f(std::string) 一切都适用于 python2 和 python3

如何修复此错误?

标签: boost-python

解决方案


推荐阅读