报错、原因、解决办法:
error: incompatible types in assignment of ‘int*’ to ‘int [2]’
原因:
将函数返回值的类型“int*”强制转化为“int[2]”类型失败
解决办法:
用一个“int*”的中间变量接收函数返回值,再转化为“int[2]”类型
修改前c++代码:
参数声明部分:
//参数声明
double point_start[3]={0.0,0.0,0.0};//起点位姿
double map_origin[2]={0.0,0.0};//地图原点
float resolution;//地图分辨率
int point_start_index[2]={0,0};//起点在地图中的坐标索引
计算坐标索引的函数(返回值为int *类型)
int * compute_point_index(double point[]){
int point_index[2];
point_index[0]=(point[0]-map_origin[0])/resolution;
point_index[1]=(point[1]-map_origin[1])/resolution;
return point_index;
}
主程序:
//主程序
point_start_index=compute_point_index(point_start);
修改后c++代码:
函数部分:修改第二行数组类型,保证在跳出函数时数组没有被销毁:
int * compute_point_index(double point[]){
static int point_index[2];
point_index[0]=(point[0]-map_origin[0])/resolution;
point_index[1]=(point[1]-map_origin[1])/resolution;
return point_index;
}
主程序部分:
int * point_index;
point_index=compute_point_index(point_start);
point_start_index[0]=*point_index;point_start_index[1]=*(point_index+1);