【问题标题】:Swig: How to typemap c++ string const & to python string?Swig:如何将 c++ 字符串 const & 类型映射到 python 字符串?
【发布时间】:2017-10-03 14:55:08
【问题描述】:

我有一个带有这些原型的构造函数:

YCPTerm(const string& s);
YCPTerm(const string& s, const YCPList& l);
YCPTerm(bytecodeistream & str);

我正在使用 swig 来生成 python 绑定。在python中,我尝试调用构造函数,得到这个:

>>> import ycp
>>> ycp.YCPTerm("Empty")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ycp.py", line 575, in __init__
    this = _ycp.new_YCPTerm(*args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'new_YCPTerm'.
  Possible C/C++ prototypes are:
    YCPTerm::YCPTerm(string const &)
    YCPTerm::YCPTerm(string const &,YCPList const &)
    YCPTerm::YCPTerm(bytecodeistream &)

我发现this answer 似乎是一个类似的问题,但类型映射类型检查对我不起作用。

这是我尝试过的:

%typemap(typecheck,precedence=141) const std::string& str {
  $1 = Z_TYPE_PP($input) == IS_STRING;
}

我该如何解决这个问题?我需要输入和输出类型图来转换值吗?我一直是reading about typemap's here,但不确定如何对 const string& 进行类型映射。

【问题讨论】:

    标签: python c++ swig


    【解决方案1】:

    std_string.i 库包含在 std::string 和 Python 字符串之间来回转换的 typedef。详情请见the documentation on the std::string library in SWIG

    【讨论】:

      【解决方案2】:

      我找到了answer here

      将这些行添加到我的 .i 文件中修复了错误:

      %include std_string.i
      %inline %{
      using namespace std;
      %}
      

      我不明白为什么。也许其他人可以回答这个问题。

      【讨论】:

        猜你喜欢
        • 2016-11-26
        • 2021-06-19
        • 1970-01-01
        • 2021-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-21
        • 1970-01-01
        相关资源
        最近更新 更多