Hi I am trying to init an object with a double value in the format double filter[3][3];
but i keep getting the following error.
cannot convert ‘double[3][3]’ to ‘double’ in assignment.
in my header i have this
@interface filter : NSObject
{
double **matrix;
}
@property(nonatomic)double **matrix;
-(id)initWithMatrix:(double**)filterMatrix;
inside my class i have this method.
-(id)initWithMatrix:(double**)filterMatrix
{
matrix = filterMatrix;
return self;
}
and i am trying to call it like this.
double filter[3][3] = {0,0,0,0,1,0,0,0,0};
MyMatrix *myMatrix = [[MyMatrix alloc] initWithMatrix:filter];
I now get the error.
Error: Cannot convert double[*][3] to double** in argument passing
Any help on this issue would be amazing.
Thanks
A
A two-dimensional array is not the same thing as a pointer-to-a-pointer. You have two choices – change the
filterclass to contain a 2D array, or change your initialization to use pointer-to-pointers.In choice #1, you’re could keep a copy of the array in your
filterinstance, instead of just holding a pointer. You need to change the class interface:Then your implementation of
initWithMatrix:can just do amemcpy()or the equivalent to copy the data into your instance.Choice #2 is a bit different. Keep your other code the way it is, but change your initialization of
filter:It’s probably safer to
malloc()all of those arrays, since otherwise you’re going to end up with references to stack variables in yourfilterclass, but I think you get the idea.