1

I am new to using JNI and so I am trying to get a double and an unsigned int from a native library in C and return the values to my java side but I keep getting the following errors from Android Studio

Error:(111, 37) error: called object type 'double' is not a function or function pointer
Error:(117, 43) error: called object type 'unsigned int' is not a function or function pointer
Error:(220, 19) error: functions that differ only in their return type cannot be overloaded

Here is my code:

double SuperpoweredExample::getPosition() {

    double pos = playerA->positionMs();

    return pos;
}

unsigned int SuperpoweredExample::getDuration() {
    unsigned int dur = playerA->durationMs();
    return dur;
}

while this is the Extern C structure

JNIEXPORT jdouble Java_com_superpowered_crossexample_MainActivity_getPosition(JNIEnv *javaEnvironment, jobject self) {

    return example->getPosition();
}
JNIEXPORT jint Java_com_superpowered_crossexample_MainActivity_getDuration(JNIEnv *javaEnvironment, jobject self) {
    return example->getDuration();
}

Please guys, I will appreciate any help at all... Thank you in advance

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
  • 1
    Java does not have signed and unsigned variants of integer types. All Java integer types except `char` are signed. Therefore, you cannot return an `unsigned int` *per se* to Java; you must convert it to a Java-supported type. In principle, you might need to convert a C `double`, too, but in practice, `double` and JNI `jdouble` are almost certainly the same type. – John Bollinger Jun 27 '17 at 20:55
  • Can you show the Java code resulting in the errors? You're ... not trying to compile the C code with a Java compiler, right? – Andy Thomas Jun 27 '17 at 20:56
  • 1
    With that said, the relationship between the error messages and the code presented is unclear. For the greatest likelihood of getting useful help, present a [mcve] that demonstrates the problem. – John Bollinger Jun 27 '17 at 20:57
  • 1
    To which lines of code do those error messages refer? It seems that you may be attempting to treat data members as functions. They aren't. You can't call them. NB There is no 'external C structure' here. – user207421 Jun 27 '17 at 22:29
  • EJP, I keep getting flagged at the playerA->positionMs; that double is "not a function or function pointer" in that which it is supposed to return a double value according to the native library's documentation I am working with. – Isaac Oyewole Jun 30 '17 at 16:58
  • Now I really don't know if the error is actually from how I structured the JNI Export or extern C or how I declared the double and unsigned int methods – Isaac Oyewole Jun 30 '17 at 17:00
  • @AndyThomas, I appreciate your feedback so much, but I am using Android Studio – Isaac Oyewole Jun 30 '17 at 17:02
  • @IsaacOyewole - Can you show the declarations of the methods `playerA->positionMs()` and `playerA->durationMs()` -- the method calls being flagged as an error? – Andy Thomas Jun 30 '17 at 17:31
  • @EJP, please how do I go about using a "data member" as a "method member" so that I can return the DOUBLE and UNSIGNED INT back to my java side, as you pointed out in your suggestions? Thanks in advance – Isaac Oyewole Jun 30 '17 at 21:33
  • @AndyThomas, thank you so much for your help, the declaration of the playerA is this: SuperpoweredAdvancedAudioPlayer *playerA; //where SuperpoweredAdvancedAudioPlayer is a class in the Superpowered SDK The documentation is here http://superpowered.com/docs/class_superpowered_advanced_audio_player.html In my implementation, I am trying to return the SuperpoweredAdvancedAudioPlayer's position to the Java side through JNI – Isaac Oyewole Jun 30 '17 at 21:40
  • 1
    @IsaacOyewole - The linked documentation shows that you're trying to access a data member like a method call, as EJP answers below. You're using `playerA->positionMs()`. Remove the parantheses, which indicate a function call -- just use `playerA->positionMs`. – Andy Thomas Jun 30 '17 at 21:53
  • @AndyThomas, it finally built the project pass that point! Thanks so much! However I do get this error now; error: functions that differ only in their return type cannot be overloaded The error flags out my JNIEXPORT line here JNIEXPORT jdouble Java_com_superpowered_crossexample_MainActivity_getPosition(JNIEnv *javaEnvironment, jobject self) { return example->getPosition (); } – Isaac Oyewole Jul 01 '17 at 06:20
  • I also tried removing the parentheses from return example->getPosition; but the error remains – Isaac Oyewole Jul 01 '17 at 06:21
  • That error suggests that you have two different definitions of getPosition(), which differ only in the return type. You have to use parantheses when you call a method or function. You may want to read an introductory text on C/C++ programming. – Andy Thomas Jul 01 '17 at 06:30
  • I think I may have just solved the last issue. I included jdouble in the JNIEXPORT side. Once again I want to really thank you @AndyThomas, if not for your help I might not have gotten this far. Thank you so much. Now I just can't find the upvote botton on your help – Isaac Oyewole Jul 01 '17 at 06:38
  • 1
    You can click the checkmark on EJP's answer below, to accept his answer. – Andy Thomas Jul 01 '17 at 06:43

1 Answers1

4

You haven't exactly troubled yourself to provide the complete class definition, or to answer the questions you've been asked in comments, but it appears that

playerA->positionMs

is a data member, not a member method, and similarly for the other method call. So you can't call them as methods. But you can return them directly as values.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I'm sorry about that @EJP, but I'm am using a native SDK library from which I created an instance of a class which is supposed to return a "double" value... I have been searching the documentation so I can properly respond to the help provided here as I am very new to native c programming. The variable class I queried in the DOUBLE method "getPosition" is said to return a double value but I keep getting the build error that double is not a function or function pointer and the line playerA->positionMs; was flagged out. – Isaac Oyewole Jun 30 '17 at 16:55
  • So it's a variable, as I said, not a function, as your code says. You need to post the definition of those variables/functions/whatever they are to clarify this. – user207421 Jul 01 '17 at 06:19