通过2-D阵列功能给出奇怪的错误
您好我想创建一个功能,但它给误差
Hi I am trying to create a function, but it gives error
[Error] cannot convert 'float (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'float (*)[30]' for argument '1' to 'float vecino_mas_cercano(float (*)[30])'
我的code是:
My code is:
#include <iostream>
#include<stdio.h>
#include <fstream>
#include<stdlib.h>
using namespace std;
int n1=30;
float nearest_neighbor(float distances[30][30])
{ float min=distances[0][1];
int candidates[n1];
int city=0;
for (int i=0;i<n1;i++)
{ candidates[i]=i;
}
for (int i=0;i<n1;i++)
{ for (int &j:candidates)
{ if (j!=0)
if (distances[city][j]<min);
{ min=distances[i][j];
candidates[i]=0;
city=j;
}
}
}
return min;
}
int main()
{
//Declaración de variables
int n=30; //número de nodos o cityes
FILE *d;
float distance[n][n];
d =fopen("Oli30.csv","r");//r abrir el archivo que contiene las distances para leerlo
for(int i=0 ;i<n; i++) //en la matriz distance se guardan los valores de las distances
{ for(int j=0; j<n; j++)
{ fscanf(d,"%f",&distance[i][j]);}
}
float tau_initial;
tau_initial=nearest_neighbor(distance);
cout << "The result is " << tau_initial;
return 0;
}
我想这个问题是因为距离设置为指针,我不知道如何传递函数nearest_neighbor这种说法。
I suppose the problem is because distance is set as a pointer, I dont know how to pass this argument in function nearest_neighbor.
有人可以解释我如何使这个工作?
Can somebody explain me how to make this works??
感谢您
INT N = 30;
必须改成 INT常量N = 30 ;
当你拥有了它,现在,浮动距离[N] [N]
是非法的。在标准C ++数组必须有维度的常量。
As you have it now, float distance[n][n]
is illegal. Arrays in Standard C++ must have constants for the dimension.
该错误信息表明,你的编译器正在实施某种形式的扩展(或许类似于C沃拉斯)与非常维数组,然后它不知道如何搭配,要它期望数组功能不断的尺寸。
The error message suggests that your compiler is implementing some kind of extension (perhaps similar to C VLAs) for an array with non-constant dimensions, and then it doesn't know how to match that to a function which expects an array of constant dimensions.