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

我使用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文件中。 有关详细信息,请参阅http://www.swig.org/Doc1.3/Library.html#Library_stl_cpp_library 。