将SWIG与以std::string为参数的方法一起使用

Using SWIG with methods that take std::string as a parameter

本文关键字:方法 一起 参数 SWIG 与以 std string      更新时间:2023-10-16

我使用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;
}

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

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

您需要包括

%include "std_string.i" 

在您的.i文件中。参见:

  • STL/C++库

    了解更多详细信息。