将SWIG与以std::string为参数的方法一起使用
Using SWIG with methods that take std::string as a parameter
我使用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++库
了解更多详细信息。
相关文章:
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 出于什么目的,非虚拟方法将与C++一起使用?
- 如何在Visual Studio 2017上将类方法设置为参数并将它们与lambda一起使用?
- 将派生类方法与基类unique_ptr一起使用
- 让类与运算符一起工作更简单的方法
- 将赋值运算符与make_pair方法一起使用会生成 CLion 警告
- 有没有一种干净(更)的方法将 CRTP 与可变参数继承混合在一起?
- 与其所有者一起分配阵列的正确方法
- 将方法与模板一起使用
- 有没有一种方法可以将QVariant与QVector一起使用
- 如何将策略模式与派生类中的其他方法一起使用
- 有什么方法可以将字符串文字u8与变量一起使用吗
- 有没有一种方法可以将升压信号和插槽与不可复制的对象一起使用
- 具有与专用列表一起使用的参数列表的超类方法<superclass>
- 为什么将我的打印方法与std::cout一起使用会导致错误
- C++ 继承函数指针,可与派生方法一起使用
- 为什么点运算符(.)可以与私有类成员/方法一起使用
- 将 boost::function 与实例方法一起使用
- 这是将抽象类与OO方法一起使用的正确方法吗?
- 将SWIG与以std::string为参数的方法一起使用