2

If it is a duplicate question just let me know, don't downvote, I am new to Android-native development. I want to create an ArrayList by using Android-native code, I tried the following:

#include <jni.h>
#include <android/log.h>
#include <vector>

template<class T>

extern "C"{  //here it is showing **error expected unqualified-id before string constant**

std::vector<T> list;

JNIEXPORT void JNICALL Java_com_example_nativetestapp_NativeList_add(
    JNIEnv * env, jobject obj, T t) {
list.push_back(t);
 }

JNIEXPORT jboolean JNICALL Java_com_example_nativetestapp_NativeList_remove(
    JNIEnv * env, jobject obj, int pos) {
if (pos > list.size() - 1 || pos < 0)
    return false;
return list.erase(list.begin() + pos) != NULL ? true : false;
 }

JNIEXPORT jint JNICALL Java_com_example_nativetestapp_NativeList_size(
    JNIEnv * env, jobject obj) {
return list.size() == NULL ? 0 : list.size();
}

JNIEXPORT jint JNICALL Java_com_example_nativetestapp_NativeList_get(
    JNIEnv * env, jobject obj, int pos) {
return list[pos];
 }

JNIEXPORT jboolean JNICALL Java_com_example_nativetestapp_NativeList_contains(
    JNIEnv * env, jobject obj, T t) {
for (int var = 0; var < list.size(); var++) {
    if(t==list[var])
        return true;
 }
return false;
}

JNIEXPORT jboolean JNICALL Java_com_example_nativetestapp_NativeList_remove(
    JNIEnv * env, jobject obj, T t) {
for (int var = 0; var < list.size(); var++) {
        if(t==list[var]){
            list.erase(list.begin()+var);
            return true;
        }
    }
    return false;
}
};

But I am stuck with the error mentioned in the code. If I place a semicolon after

template<class T>;

then after this line I get a new error:

cannot resolve T symbol.

1 Answer 1

1

add extern "C" before every JNIEXPORT, rather than wrap all codes.

Sign up to request clarification or add additional context in comments.

5 Comments

pull all vector operation to static functions. take size() for example, add a function like: 'static int v_size(){return list.size();}`
then i think i have to take vector also static right?
no dude it is not working i think it is template problem, is it correct way use template?
no problem to use template in c++ codes. but just not use it in extern "c" scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.