我有一个c ++类,一个成员函数使用两个double数组作为输入,如:
class model{
//define some varible..
void Trainmodel(double *x,double *y);
//...
};
我想在c#中使用此类,并按照./swigwin2.0.9/examples/csharp/arrays
中的示例和帮助页面中的指南进行操作:SWIG
我的model.MY文件就像:
%module model_dll
%{
/* Includes the header in the wrapper code */
#include "model.h"
%}
/* Parse the header file to generate wrappers */
%include "model.h"
%include "arrays_csharp.i"
%apply double INPUT[] { double* x }
%apply double INPUT[] { double* y }
但是当我在c#中使用此函数时,会发生错误:
错误1 model.Trainmodel(SWIGTYPE_p_double, SWIGTYPE_p_double)
的最佳重载方法匹配包含一些无效参数
和
错误2参数1:无法从double[]
转换为SWIGTYPE_p_double
我查看了model.cs的源代码,发现了trainmodel函数如下:
public int Trainmodel(SWIGTYPE_p_double x, SWIGTYPE_p_double y) {
//do something
}
任何人都可以帮我弄清楚这些代码有什么问题吗?为什么swig生成SWIGTYPE_p_double
而不是double []
?
我在下面的SWIG中复制示例代码:
c#code:
int[] source = { 1, 2, 3 };
int[] target = new int[ source.Length ];
example.myArrayCopy( source, target, target.Length );
c code:
void myArrayCopy( int* sourceArray, int* targetArray, int nitems ) {
int i;
for ( i = 0; i < nitems; i++ ) {
targetArray[ i ] = sourceArray[ i ];
}
}
.i warp文件:
%include "arrays_csharp.i"
%apply int INPUT[] {int *sourceArray}
%apply int OUTPUT[] {int *targetArray}
我错过了什么吗?
SORRY 我犯了这样一个愚蠢的错误。(WTF!:-()在* .i文件中,代码“%include”model.h“”应该放在这些代码“%include”arrays_csharp.i“%apply ...之后“不是之前。所以正确的形式是:
%module model_dll
%{
/* Includes the header in the wrapper code */
#include "model.h"
%}
/* Parse the header file to generate wrappers */
%include "arrays_csharp.i"
%apply double INPUT[] { double* x }
%apply double INPUT[] { double* y }
%include "model.h"//BE CAREFUL: this should put after the include"arrays_csharp.i"
问题解决了。任何想要包含arrays_csharp.i的人,请确保在包含您自己的.h文件(本例中为model.h)之前编写“include”代码和“apply”代码。