user3897831
user3897831

Reputation: 13

C++ struct to Java class

I want to convert a c++ struct like this:

typedef struct FEATUREINFO
{
    string str_ID;
    char* c_ID;
    double* featureData;
    int group;
    bool bPrint;
    IplImage *t_faceImg;
}FEATUREINFO;

And I will use it :

FEATUREINFO * p_featureNode = new FEATUREINFO[100];
for(int j=0; j<100 ; j++)
{
    p_featureNode[j].featureData = (double*)calloc(t_featureLen,sizeof(double));
    p_featureNode[j].bPrint = false;
}

In Java code I wrote my code :

class FEATUREINFO
{
    string str_ID;
    char[] c_ID;
    public Double[] featureData;
    int group;
    public boolean bPrint;
    //IplImage *t_faceImg;
    public FEATUREINFO()
    {
        this.featureData = new Double[1280] ;
    }     
} // class FEATUREINFO

And worte a simple code to test whether I success:

FEATUREINFO[] p_featureNode = new FEATUREINFO[100];
p_featureNode[5].featureData[2] = 100.5 ;  // this line will error!!! =(
Log.d(Tag_Test, "featureData :" + p_featureNode[5].featureData[2] ) ;     

I'm a beginner of Java, please help me! Thank you very much!

There is my error: https://i.sstatic.net/swow5.png

Thank you again!!!!! =D

Upvotes: 1

Views: 1818

Answers (2)

user3514233
user3514233

Reputation:

  public class FEATUREINFO(){

    string str_ID;
    char c_ID;
    double featureData;
    int group;
    bool bPrint;
    IplImage t_faceImg;

  public /*noreturntypeforconstructor*/ FEATUREINFO(String a, char b, double c, int d, bool e, IplImage f){

    this.str_ID = a; // the inputs to the constructor
    this.c_ID = b;
    this.featureData = c;
    this.group = d;
    this.bPrint = e;
    this.t_faceImg = f;

  } // if all values arenot know before object is create sostandard constructor isused (FEATUREINFO(/*no args*/))

  public String getstrID(){

    return this.str_ID;

  }

  public String setstrID(String inputString){

    this.str_ID = inputString;

  } // getter and setters for each member....

} // i understand that you probably already know all of this but it was fun writing it :)

Upvotes: 0

Dave Doknjas
Dave Doknjas

Reputation: 6542

C++ initializes each array element to a default state (calling the default constructor for each array element) - Java does not.

You need something like:

FEATUREINFO[] p_featureNode = initializeWithDefaultFEATUREINFOInstances(100);
...

public static FEATUREINFO[] initializeWithDefaultFEATUREINFOInstances(int length)
{
    FEATUREINFO[] array = new FEATUREINFO[length];
    for (int i = 0; i < length; i++)
    {
        array[i] = new FEATUREINFO();
    }
    return array;
}

Upvotes: 2

Related Questions