【问题标题】:Using SWIG with methods that take std::string as a parameter将 SWIG 与将 std::string 作为参数的方法一起使用
【发布时间】:2012-02-03 01:20:58
【问题描述】:

我使用 SWIG 来包装我的 c++ 类。有些方法有一个const std::string& 作为参数。 SWIG 创建了一个名为 SWIGTYPE_p_std__string 的类型,但是在 c# 中调用该方法时,您不能只为此传递一个普通字符串。下面的示例只是 SWIG 包附带的修改示例。:

public void setName(SWIGTYPE_p_std__string name) 
{
    examplePINVOKE.Shape_setName(swigCPtr, SWIGTYPE_p_std__string.getCPtr(name));
    if (examplePINVOKE.SWIGPendingException.Pending) throw examplePINVOKE.SWIGPendingException.Retrieve();
}

在我的接口文件中,我只有:

/* File : example.i */
%module example

%{
#include "example.h"
%}

/* Let's just grab the original header file here */
%include "example.h"

而被 C++ 包装的方法是:

void Shape::setName(const std::string& name)
{
    mName = name;
}

我必须在接口文件中放入某种类型的映射吗?如果是这样,我该怎么做?

【问题讨论】:

    标签: c# c++ swig


    【解决方案1】:

    当我发现你的问题时,我正试图自己解决这个问题。

    你需要包含

    %include "std_string.i" 
    

    在您的 .i 文件中。见:

    【讨论】:

      猜你喜欢
      • 2011-01-03
      • 2015-09-15
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      • 2020-12-05
      相关资源
      最近更新 更多