I have a c++ class and a member function uses two double array as input like:
class model{
//define some varible..
void Trainmodel(double *x,double *y);
//...
};
I wanted to use this class in c# and followed the example in ./swigwin2.0.9/examples/csharp/arrays
and the guide in help page: SWIG
my model.MY file is like:
%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 }
but when I use this function in c#, an error occurs:
Error 1 The best overloaded method match for model.Trainmodel(SWIGTYPE_p_double, SWIGTYPE_p_double)
has some invalid arguments
and
Error 2 Argument 1: cannot convert from double[]
to SWIGTYPE_p_double
I check the source code of model.cs and found the trainmodel function like this:
public int Trainmodel(SWIGTYPE_p_double x, SWIGTYPE_p_double y) {
//do something
}
could anyone help me figure out what's wrong with these codes? Why swig generate SWIGTYPE_p_double
instead of double []
?
I copy the example code in SWIG below:
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 file:
%include "arrays_csharp.i"
%apply int INPUT[] {int *sourceArray}
%apply int OUTPUT[] {int *targetArray}
am I missing something?
SORRY I made such a stupid mistake.(WTF! :-( ) in the *.i file, the code " %include "model.h" " should put after these code "%include "arrays_csharp.i" %apply ..." not before. So the right form is:
%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"
the problem is solved. and anyone who want include arrays_csharp.i, please make sure the "include" code and the "apply" code is written before include your own .h file (model.h in this case).