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

Set*_*eth 17 c# c++ swig

我使用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();
}
Run Code Online (Sandbox Code Playgroud)

在我的界面文件中我只有:

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

%{
#include "example.h"
%}

/* Let's just grab the original header file here */
%include "example.h"
Run Code Online (Sandbox Code Playgroud)

并且用C++包装的方法是:

void Shape::setName(const std::string& name)
{
    mName = name;
}
Run Code Online (Sandbox Code Playgroud)

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

pup*_*pet 21

当我找到你的问题时,我正试图自己解决这个问题.

你需要包括

%include"std_string.i"

在你的.i文件中.有关详细信息,请参阅http://www.swig.org/Doc1.3/Library.html#Library_stl_cpp_library.