我的Android项目中有以下代码(存储为.c文件):
static void convert(unsigned int &c, const float &temp1, const float &temp2, const float &temp3) {
if ( (temp3 * 6) < 1) c = (unsigned int) ((temp2 + (temp1 - temp2) * 6 * temp3) * 100);
else if ((temp3 * 2) < 1) c = (unsigned int) (temp1 * 100);
else if ((temp3 * 3) < 2) c = (unsigned int) ((temp2 + (temp1 - temp2) * (.66666 - temp3) * 6) * 100);
else c = (unsigned int) (temp2 * 100);
return;
}
在代码执行过程中,其调用方式如下:
convert(r, temp1, temp2, temp3);
但是,当我从命令行使用 ndk-build 命令对其进行编译时,会出现以下错误:
Android NDK:未设置APP_PLATFORM。默认为最小支持 版本android-16。 [arm64-v8a]编译: com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor <= com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:193:47: 错误: 预期')'静态void convert(unsigned int&c,const float&temp1,const float ... ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:193:33: 注意: 匹配这个'('static void convert(unsigned int&c,const float&temp1,const float ... ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:193:47: 错误: 参数名称被省略static void convert(unsigned int&c,const float&temp1,const float ... ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:194:11: 错误: 使用未声明的标识符“ temp3” if((temp3 * 6)<1)c =(无符号整数)((temp2 +(temp1-temp2)* 6 * temp3 ... ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:194:27: 错误: 使用未声明的标识符“ c” if((temp3 * 6)<1)c =(无符号整数)((temp2 +(temp1-temp2)* 6 * temp3 ... ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:194:57: 错误: 使用未声明的标识符“ temp1” if((temp3 * 6)<1)c =(无符号整数)((temp2 +(temp1-temp2)* 6 * temp3 ... ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:194:65: 错误: 使用未声明的标识符“ temp2” if((temp3 * 6)<1)c =(无符号整数)((temp2 +(temp1-temp2)* 6 * temp3)* ... ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:194:48: 错误: 使用未声明的标识符“ temp2” if((temp3 * 6)<1)c =(无符号整数)((temp2 +(temp1-temp2)* 6 * temp3 ... ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:194:78: 错误: 使用未声明的标识符'temp3'...(temp3 * 6)<1)c =(unsigned int)((temp2 +(temp1-temp2)* 6 * temp3)* 100); ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:196:15: 错误: 使用未声明的标识符“ temp3” 否则((temp3 * 2)<1)c =(无符号整数)(temp1 * 100); ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:196:31: 错误: 使用未声明的标识符“ c” 否则((temp3 * 2)<1)c =(无符号整数)(temp1 * 100); ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:196:51: 错误: 使用未声明的标识符“ temp1” 否则((temp3 * 2)<1)c =(无符号整数)(temp1 * 100); ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:198:15: 错误: 使用未声明的标识符“ temp3” 否则((temp3 * 3)<2)c =(无符号整数)((temp2 +(temp1-temp2)*(.666 ... ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:198:31: 错误: 使用未声明的标识符“ c” 否则((temp3 * 3)<2)c =(无符号整数)((temp2 +(temp1-temp2)*(.666 ... ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:198:61: 错误: 使用未声明的标识符“ temp1” 否则((temp3 * 3)<2)c =(无符号整数)((temp2 +(temp1-temp2)*(.666 ... ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:198:69: 错误: 使用未声明的标识符'temp2'... if((temp3 * 3)<2)c =(unsigned int)((temp2 +(temp1-temp2)*(.66666-temp ... ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:198:52: 错误: 使用未声明的标识符“ temp2” 否则((temp3 * 3)<2)c =(无符号整数)((temp2 +(temp1-temp2)*(.666 ... ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:198:88: 错误: 使用未声明的标识符'temp3'... <2)c =(unsigned int)((temp2 +(temp1-temp2)*(.66666-temp3)* 6)* 100); ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:200:10: 错误: 使用未声明的标识符“ c” 否则c =(unsigned int)(temp2 * 100); ^ jni / com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:200:30: 错误: 使用未声明的标识符“ temp2” else c =(unsigned int)(temp2 * 100);
我从.cpp文件中获得了该代码。当我删除签名中的地址运算符(&)时,一切都很好(没有错误),但是我不知道该算法是否可以工作。
有人可以帮忙吗?
答案 0 :(得分:1)
您正在尝试使用C编译器来编译C ++代码,这将无法正常工作。
显而易见的解决方案是改为使用C ++编译器进行编译(例如,通过为源文件提供.cpp
扩展名)。
如果由于某种原因必须使用C编译器,则必须用C重写该函数。
对于初学者来说,您可以按值而不是按引用传递所有这些const float
参数(通过摆脱&
)。我不知道为什么它们首先是引用。
该第一个参数(c
)用作将结果写入其中的输出参数。因此,您必须将其从引用更改为指针(即unsigned int *c
)。或者更好的方法是,跳过输出参数,让函数返回其结果:
static int convert(const float temp1, const float temp2, const float temp3) {
unsigned int c;
if ( (temp3 * 6) < 1) c = (unsigned int) ((temp2 + (temp1 - temp2) * 6 * temp3) * 100);
else if ((temp3 * 2) < 1) c = (unsigned int) (temp1 * 100);
else if ((temp3 * 3) < 2) c = (unsigned int) ((temp2 + (temp1 - temp2) * (.66666 - temp3) * 6) * 100);
else c = (unsigned int) (temp2 * 100);
return c;
}
呼叫代码:
r = convert(temp1, temp2, temp3);
答案 1 :(得分:0)
在C ++中,unsigned int &c
是参考参数,在这种情况下,&
不是“地址运算符”。但是,引用在C中不存在,这就是为什么在.c文件而不是.cpp