[SOLVED] Using custom C++ classes

Put problems with the Android NDK in this forum.

[SOLVED] Using custom C++ classes

Postby gskbyte » Tue May 25, 2010 11:49 am

Hi!
I'm investigating on the NDK and I want to create a "Spline" class that runs on native code. I have a C++ class from another project (with its .h and .cpp files) and I would like to use them in my Android project, because it offers more functionality than Android's "Path" class.

The problem is that I want to port some code in C++ but most examples are made in C. What changes do I need to make to .h and .cpp text files? Currently, my test app throws an UnsatisfiedLinkError when I call to a native method that has the same name and parameters of the Java class.

By the way, do you know any C++ example to get some inspiration?

Thank you!
Last edited by gskbyte on Wed May 26, 2010 11:28 am, edited 1 time in total.
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Top

Re: Using custom C++ classes

Postby gskbyte » Wed May 26, 2010 11:28 am

Okay, I've solved it. It is not possible to directly use custom classes, you've got to wrap them with JNI calls and keep a pointer inside the Java class. If someone is interested on what I did, I can post sourcecode here.
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Re: [SOLVED] Using custom C++ classes

Postby luugiathuy » Fri Jun 04, 2010 3:17 am

Hi,

I'm interested in this problem. Can you post your source code here? Thank you.
luugiathuy
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jun 01, 2010 3:16 am

Re: [SOLVED] Using custom C++ classes

Postby gskbyte » Fri Jun 04, 2010 4:22 pm

Hi,

I've made a little JNI example. If you see the code, the only thing I do is to wrap C++ class methods inside a Java class, keeping a pointer inside it.

I have also included a little builder helper in the project (which compiles automatically the C++ source code), but it only works on GNU/Linux, inside a defined folder (in my case /home/jose/workspace/JNITest). If it causes problems to you, remove it following these steps:

Right-click on project->Properties->Builders, and then remove (or edit) the last one ("Native builder").

I hope this helps you!
Attachments
JNIExample.tar.gz
(96.41 KiB) Downloaded 325 times
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Re: [SOLVED] Using custom C++ classes

Postby luugiathuy » Fri Jun 04, 2010 4:39 pm

Hi,

It works fine for me =). Thank you very much.
luugiathuy
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jun 01, 2010 3:16 am

Re: [SOLVED] Using custom C++ classes

Postby Gabrielwer » Wed Sep 07, 2011 2:02 pm

Hi there,

I have a custom class , lets call it MyClass. In the main part of my prog I want to run a cout which outputs say Myinstance. I would have though I would add it nt he stdiolib. But I want to add it in my class... Something along the lines of

class MyClass {

MyClass::Cout {
print. MyInstance.x + " " + MyInstance.y;
}
}

Something like that, I'm not doing it properly as, it is giving me the error: "no match for operator<< in std::operator<<
Gabrielwer
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Sep 07, 2011 2:00 pm

Top

Return to NDK Problems

Who is online

Users browsing this forum: No registered users and 3 guests