c++结构到Java类

C++ struct to Java class

本文关键字:Java 结构 c++      更新时间:2023-10-16

我想这样转换一个c++结构体:

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

我将使用它:

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

在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
并写了一个简单的代码来测试我是否成功:
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] ) ;     

我是Java的初学者,请帮助我!非常感谢!

这是我的错误:https://i.stack.imgur.com/swow5.png

再次感谢!!!!!div = D

c++将每个数组元素初始化为默认状态(调用每个数组元素的默认构造函数)- Java不这样做。

你需要这样写:

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