简体   繁体   English

C ++结构到Java类

[英]C++ struct to Java class

I want to convert a c++ struct like this: 我想这样转换c ++结构:

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 : 在Java代码中,我编写了代码:

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! 我是Java的初学者,请帮助我! Thank you very much! 非常感谢你!

There is my error: http://i.imgur.com/lwUaSTg.png 我的错误是: http : //i.imgur.com/lwUaSTg.png

Thank you again!!!!! 再次感谢你!!!!! =D = d

C++ initializes each array element to a default state (calling the default constructor for each array element) - Java does not. C ++将每个数组元素初始化为默认状态(为每个数组元素调用默认构造函数)-Java不会。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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