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

﹥>﹥吖頭↗ 提交于 2019-12-04 17:12:37

问题


I used SWIG to wrap my c++ class. Some methods have a const std::string& as a parameter. SWIG creates a type called SWIGTYPE_p_std__string however you cannot just pass a normal string for this when invoking the method in c#. The below example is just a modified example that comes with the SWIG package.:

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();
}

In my interface file I just have:

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

%{
#include "example.h"
%}

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

And the method that is being wrapped in C++ is:

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

Is there some sort of typemap I have to put in the interface file? If so, how do I do that?


回答1:


I was trying to solve this myself when I found your question.

You need to include

%include "std_string.i" 

in your .i file. See:

  • STL/C++ library

    for more details.



来源:https://stackoverflow.com/questions/9122511/using-swig-with-methods-that-take-stdstring-as-a-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!