简体   繁体   中英

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: http://i.imgur.com/lwUaSTg.png

Thank you again!!!!! =D

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;
}
  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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM