我想爲我的論文開發一個代碼,並且我正試圖學習如何將二維數組傳遞給一個函數。我寫了一些東西。代碼在下面,它不是以這種形式工作的。我有這個錯誤:C++ 2d數組傳遞到一個函數
error: cannot convert ‘float (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)]’ to ‘float (*)[2]’ for argument ‘3’ to ‘void func(int, int, float (*)[2], float)’ func(m, n, a, omega);
當我改變這個矩陣的聲明float a [m] [n];浮動[2] [2]它正在工作。先謝謝你。
void func(int m1, int n1, float a1[2][2], float omeg);
using namespace std;
int main(){
int m = 2;
int n = 2;
int i, j;
float a[m][n];
float x,y,z,omega, c;
x=0.1;
y=0.2;
z=0.3;
c = 0;
omega = (x*y*z)/x;
for(j = 0; j < n; j++)
{
for(i = 0; i < m; i++)
{
a[i][j] = 0.0;
}
}
func(m, n, a, omega);
cout << a[1][0] << endl;
return 0;
}
void func(int m1, int n1, float a1[][2], float omeg)
{
for(int j = 0; j < n1; j++)
{
for(int i = 0; i < m1; i++)
{
a1[i][j] = omeg * 5;
}
}
}
使用'const'爲'M'和'N',否則您使用非標準擴展可變長度陣列(VLA)。 – Jarod42 2014-12-04 14:42:58
爲什麼不使用vector(www.cplusplus.com/reference/vector/vector/)而不是數組? – geoalgo 2014-12-04 14:44:21
我甚至沒有嘗試發佈答案,你正在嘗試使用C++ w/o有關指針和參數傳遞的任何知識。你需要在編寫代碼之前學習這些。 – 2014-12-04 14:48:28