Android将“.c”后缀名改为“.cpp”时java调用native失败及“error: base operand of '->' has non-poin...

本文详细介绍了在使用Java调用native方法时遇到的两个常见问题:'.c'后缀名改为'.cpp'时的java调用native失败,以及'error: base operand of '->' has non-pointer type '_JNIEnv'的错误,并提供了相应的解决方案。通过实例代码,读者可以清晰地理解如何避免和解决这些问题。

---------------------------------------华丽的分割线-----------------------------------------

现象“.c”后缀名改为“.cpp”时java调用native失败 。

解决 加入“ extern "C" ”。  

 

现象“error: base operand of '->' has non-pointer type '_JNIEnv'”错误。   

解决 将“(*env)->NewStringUTF(env, "HelloWorld from JNI !");”改为“env->NewStringUTF("HelloWorld from JNI !")”。

 

例子:

 

#include <stdio.h>
#include <string.h>
#include <android/log.h>
#include <jni.h>

#ifdef __cplusplus
extern "C"
{
#endif

jint Java_com_duicky_MainActivity_add(JNIEnv* env, jobject thiz, jint x, jint y)
{
	//该方法为打印的方法
	__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "Get Param:  x=%d y=%d ", x, y);

	int iRet = x + y;
	return iRet;
}

jstring Java_com_duicky_MainActivity_getString(JNIEnv* env, jobject thiz)
{
	jstring strRet = env->NewStringUTF("HelloWorld from JNI !");
	return strRet;
}

#ifdef __cplusplus
}
#endif

 

step1/lt.cpp: In function ‘void CreateGraphF(MGraph&)’: step1/lt.cpp:76:28: error: base operand of->’ has non-pointer type ‘MGraph’ fscanf(file, "%d%d", &G->vexnum, &G->arcnum); // 读入顶点数和边数 ^~ step1/lt.cpp:76:40: error: base operand of->’ has non-pointer type ‘MGraph’ fscanf(file, "%d%d", &G->vexnum, &G->arcnum); // 读入顶点数和边数 ^~ step1/lt.cpp:77:6: error: base operand of->’ has non-pointer type ‘MGraph’ G->kind = UDN; // 设置为无向网 ^~ step1/lt.cpp:80:26: error: base operand of->’ has non-pointer type ‘MGraph’ for (int i = 0; i < G->vexnum; i++) { ^~ step1/lt.cpp:81:29: error: base operand of->’ has non-pointer type ‘MGraph’ fscanf(file, "%s", G->vexs[i]); ^~ step1/lt.cpp:85:26: error: base operand of->’ has non-pointer type ‘MGraph’ for (int i = 0; i < G->vexnum; i++) { ^~ step1/lt.cpp:86:30: error: base operand of->’ has non-pointer type ‘MGraph’ for (int j = 0; j < G->vexnum; j++) { ^~ step1/lt.cpp:87:14: error: base operand of->’ has non-pointer type ‘MGraph’ G->arcs[i][j].adj = INFINITY; ^~ step1/lt.cpp:89:10: error: base operand of->’ has non-pointer type ‘MGraph’ G->arcs[i][i].adj = 0; // 自身距离为0 ^~ step1/lt.cpp:93:26: error: base operand of->’ has non-pointer type ‘MGraph’ for (int k = 0; k < G->arcnum; k++) { ^~ step1/lt.cpp:98:27: error: no match for ‘operator*’ (operand type is ‘MGraph’) int i = LocateVex(*G, v1); ^~ step1/lt.cpp:99:27: error: no match for ‘operator*’ (operand type is ‘MGraph’) int j = LocateVex(*G, v2); ^~ step1/lt.cpp:106:10: error: base operand of->’ has non-pointer type ‘MGraph’ G->arcs[i][j].adj = weight; ^~ step1/lt.cpp:107:10: error: base operand of->’ has non-pointer type ‘MGraph’ G->arcs[j][i].adj = weight; // 无向图对称 ^~ step1/lt.cpp: At global scope: step1/lt.cpp:116:1: error: expected declaration before ‘}’ token } ^
最新发布
11-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值