如何将矩阵传递给函数

如何将矩阵传递给函数

问题描述:

我的程序出错。错误信息是


e:\ test\test\main.cpp(9):错误C2664:''有趣'':无法转换参数1

从''int [2] [3]''到''int **''


如果我改变参数int ** a到int a [2] [3],就可以了。但是,我不想改变定义。

如何将矩阵传递给函数?


// ----------------------------------------------- --------------

void fun(int ** a,const int row,const int col);


void main()

{


int a [2] [3];

fun(a,2,3 );

}


void fun(int ** a,const int row,const int col)

{

for(int i = 0; i< row; ++ i)

for(int j = 0; j< col; ++ j)

a [i] [j] = i * 10 + j;

}

There is an error in my program. The error message is

e:\test\test\main.cpp(9): error C2664: ''fun'' : cannot convert parameter 1
from ''int [2][3]'' to ''int ** ''

If I change the argument "int** a" to "int a[2][3], it will be OK. But, I
don''t want to change the definition.
How can I pass the matrix to the function?

//-------------------------------------------------------------
void fun(int **a,const int row,const int col);

void main()
{

int a[2][3];
fun(a,2,3);
}

void fun(int **a,const int row,const int col)
{
for(int i=0;i<row;++i)
for(int j=0;j<col;++j)
a[i][j]=i*10+j;
}

尝试制作你的int * * a进入int(* a)[3]这样你就可以告诉

计算机你想要一个指向一个有3个int的数组的指针。如果你有一个** b $ b和**这只是双重间接,并且用(*)[]你有一个指针

到一个数组。


HTH


-

Frane Roje


祝你有愉快的一天>

从电子邮件中删除(d * el * ete)以回复
Try making your int **a into an int (*a)[3] this way you actualy tell the
computer you want a pointer to an array that has a 3 ints in it. If you had
an **int this is just double indirection, and with(*)[] you have a pointer
to an array.

HTH

--
Frane Roje

Have a nice day

Remove (d*el*ete) from email to reply


尝试将int ** a变为int(* a) [3]这样你就可以告诉

计算机你想要一个指针,指向一个有3个整数的数组。如果你有一个** b $ b和**这只是双重间接,并且用(*)[]你有一个指针

到一个数组。


HTH


-

Frane Roje


祝你有愉快的一天>

从电子邮件中删除(d * el * ete)以回复

Try making your int **a into an int (*a)[3] this way you actualy tell the
computer you want a pointer to an array that has a 3 ints in it. If you had
an **int this is just double indirection, and with(*)[] you have a pointer
to an array.

HTH

--
Frane Roje

Have a nice day

Remove (d*el*ete) from email to reply


在文章< ca ***** ******@news.ntust.edu.tw&gt ;, Wei-Chao Hsu写道:
In article <ca***********@news.ntust.edu.tw>, Wei-Chao Hsu wrote:
我的程序中有错误。错误信息是

e:\\\\test \ main.cpp(9):错误C2664:''有趣'':无法转换参数1
来自''int [2] [3]''到''int **''


这是无法做到的。 int [2] [3]不存储为a **。它作为数组存储了

,每个元素都作为int [3]。你可以定义fun()如

这个:


void fun(int a [] [3],const int row)

{

for(...)a(...)a [i] [j] = ...;

}


注意除了第一个索引之外的所有索引都需要在编译时知道。你可以用
来定义fun():


void fun(int * a,const int row,const int col)

{

for(int i = 0; i< row; ++ i)for(int j = 0; j< col; ++ j)

a [i * col + j] = i * 10 + j;

}


但是必须这样称呼乐趣:


fun(reinterpret_cast< int *>(a),2,3)


有多种方法可以做,但是:C ++有一些东西叫做

班。使用此功能比使用
普通数组更好地实现矩阵。

如果我更改参数int ** a到int a [2] [3],就可以了。但是,我不想改变定义。
如何将矩阵传递给函数?


你需要改变乐趣的定义。

// ------------------ -------------------------------------------
虚无趣( int ** a,const int row,const int col);

void main()


int main()

{

int a [2] [3];
有趣(a,2,3);
}

无趣的乐趣(int ** a,const int row,const int col)
{
for(int i = 0; i< row; ++ i)
for(int j = 0; j< col; + + j)
a [i] [j] = i * 10 + j;
}
There is an error in my program. The error message is

e:\test\test\main.cpp(9): error C2664: ''fun'' : cannot convert parameter 1
from ''int [2][3]'' to ''int ** ''
That can''t be done. int a[2][3] is not stored as an a**. It''s stored
as an array, whith each element as an int[3]. You could define fun() like
this:

void fun(int a[][3], const int row)
{
for (...) for (...) a[i][j]=...;
}

Note all but the first index needs to be known at compile time. You
could also define fun() like this:

void fun(int *a, const int row, const int col)
{
for (int i=0;i<row;++i) for (int j=0;j<col;++j)
a[i*col + j] = i*10+j;
}

but then fun must be called as this:

fun(reinterpret_cast<int*>(a), 2, 3)

There is more than one way to do things, but: C++ has something called
classes. To implement a matrix is better done using this feature than
plain arrays.
If I change the argument "int** a" to "int a[2][3], it will be OK. But, I
don''t want to change the definition.
How can I pass the matrix to the function?
You would need to change the definition of fun.
//-------------------------------------------------------------
void fun(int **a,const int row,const int col);

void main()
int main()
{

int a[2][3];
fun(a,2,3);
}

void fun(int **a,const int row,const int col)
{
for(int i=0;i<row;++i)
for(int j=0;j<col;++j)
a[i][j]=i*10+j;
}



-

Robert Bauck哈马尔


--
Robert Bauck Hamar