如何将double [] []传递给方法调用? [英] How to pass double[][] into a method call?
问题描述
您好我正在尝试使用double filter [3] [3]格式初始化一个具有double值的对象;
但是我一直收到以下错误。
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.
在分配时无法将'double [3] [3]'转换为'double'。
cannot convert 'double[3][3]' to 'double' in assignment.
在我的标题中我有这个
@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;
}
我试图这样称呼它。
double filter[3][3] = {0,0,0,0,1,0,0,0,0};
MyMatrix *myMatrix = [[MyMatrix alloc] initWithMatrix:filter];
我现在收到错误。
Error: Cannot convert double[*][3] to double** in argument passing
这个问题的任何帮助都会很棒。
谢谢
A
Any help on this issue would be amazing. Thanks A
推荐答案
二维数组与指针指向不同-a指针。您有两种选择 - 更改过滤器
类以包含2D数组,或更改初始化以使用指针指针。
A two-dimensional array is not the same thing as a pointer-to-a-pointer. You have two choices - change the filter
class to contain a 2D array, or change your initialization to use pointer-to-pointers.
在#1选项中,您可以在过滤器
实例中保留数组的副本,而不是只是拿着一个指针。您需要更改类接口:
In choice #1, you're could keep a copy of the array in your filter
instance, instead of just holding a pointer. You need to change the class interface:
@interface filter : NSObject
{
double matrix[3][3];
}
-(id)initWithMatrix:(double[3][3])filterMatrix;
然后你的 initWithMatrix:
的实现可以只是做一个 memcpy()
或相当于将数据复制到你的实例。
Then your implementation of initWithMatrix:
can just do a memcpy()
or the equivalent to copy the data into your instance.
选择#2有点不同。保持其他代码不变,但更改初始化过滤器
:
Choice #2 is a bit different. Keep your other code the way it is, but change your initialization of filter
:
double row0[3] = {0,0,0};
double row1[3] = {0,1,0};
double row2[3] = {0,0,0};
double **filter[3] = { row0, row1, row2 };
它可能更安全 malloc()
全部那些数组,因为否则你最终会在过滤器
类中引用堆栈变量,但我想你明白了。
It's probably safer to malloc()
all of those arrays, since otherwise you're going to end up with references to stack variables in your filter
class, but I think you get the idea.
这篇关于如何将double [] []传递给方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!