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

18

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

我需要在接口文件中放置某种类型映射吗?如果是这样,我该如何做到呢?

1个回答

22
我发现你的问题时,我正在尝试自己解决这个问题。 你需要包含:
%include "std_string.i" 

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接