首页 > 解决方案 > Java 到 C++ 的迁移,如何做递归类

问题描述

所以,我正在研究一个超级深入的矮人堡垒模拟类型的东西。最初,我从 java 开始,但我知道该项目最终会发展到性能成为真正问题的地步。于是,我切换到了 C++。但是,java 中调用的类之一Material似乎没有转换为 c++。在 java 版本中,该类看起来像这样:(在此之后我将有一个包含所有重要位的版本)

//import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;


public class material {
    private int melting_temp=10200;
    private int boiling_temp=10500;
    private int elastic=10;
    private int plastic=10;
    private int density=10;
    private float pourosity=0; //how much liquid the material can absorb (0 - 1)
    private material[] composite= new material[1];  // what this meta-material is built out of. 
    private char[] RGB= {255,255,255};
    
    
    
    
    public char[] get_RGB() {
        return RGB;
    }

    public void set_RGB(char[] rGB) {
        RGB = rGB;
    }

    public material() {
        //composite[0]= new material(false);
    }
    
    public material(String file, int index) throws Exception {
        String[] z=loader(file);
        String b = z[index];
        
        string_load(b.split("PUT_SPACE_HERE"));
    }
    
    
    public material(material new_material) {  // useful for copying characteristics
        melting_temp=new_material.get_melting_temp();
        boiling_temp=new_material.get_boiling_temp();
        elastic=new_material.get_elastic();
        plastic=new_material.get_plastic();
        density=new_material.get_density();
        pourosity=new_material.get_pourosity();
        composite=new_material.get_composite();
        RGB=new_material.get_RGB();
    }
    
    
    public material(int mt, int bt, int el, int pl, int dn, int pr) { // create a new material with parameters
        melting_temp=mt;
        boiling_temp=bt;
        elastic=el;
        plastic=pl;
        density=dn;
        pourosity=pr;
    }
    
    
    
    
    
    public void blend_material(material new_material) { // adds a new material to the composite
        
        //System.out.println(composite[0]);
        if(composite[0]==null) {
            composite[0]=new material(0,0,0,0,0,0);
            
        }
        composite = Arrays.copyOf(composite, composite.length+1);
        composite[composite.length-1]=new_material;
        
        melting_temp=0;
        boiling_temp=0;
        elastic=0;
        plastic=0;
        density=0;
        pourosity=0;
        for(int i=0; i<composite.length; i++) {  // set the properties of this meta-material to be the average of it's components
            melting_temp+=composite[i].get_melting_temp();
            boiling_temp+=composite[i].get_boiling_temp();
            elastic += composite[i].get_elastic();
            plastic += composite[i].get_plastic();
            density += composite[i].get_density();
            pourosity += composite[i].get_pourosity();
            for(int j=0; j<3;j++) {
                RGB[j]+=composite[i].get_RGB()[j];
            }
        }

            melting_temp/=composite.length-1;
            boiling_temp/=composite.length-1;
            elastic/=composite.length-1;
            plastic/=composite.length-1;
            density/=composite.length-1;
            pourosity/=composite.length-1;
            for(int j=0; j<3;j++) {
                RGB[j]=(char)Math.min(RGB[j]/composite.length,255);
            }
        
    }
    
    
    
    public material[] get_composite() {
        return composite;
    }
    
    public void change_melting_temp(int new_temp) {
        melting_temp=new_temp;
    }
    public void change_boiling_temp(int new_temp) {
        boiling_temp=new_temp;
    }
    public int get_melting_temp() {
        return melting_temp;
    }
    public int get_boiling_temp() {
        return boiling_temp;
    }
    public int get_plastic() {
        return plastic;
    }
    public void change_plastic(int new_plastic) {
        this.plastic= new_plastic;
    }
    public int get_elastic() {
        return elastic;
    }
    public void change_elastic(int new_elastic) {
        this.elastic= new_elastic;
    }
    public int get_density() {
        return density;
    }
    public void change_density(int new_density) {
        this.density= new_density;
    }
    public float get_pourosity() {
        return pourosity;
    }
    public void change_pourosity(int new_pourosity) {
        this.pourosity = new_pourosity;
    }
    
    public void random() {
        melting_temp=(int)(Math.random()*2000 +9000);
        boiling_temp=melting_temp+(int)(Math.random()*2000);
        elastic=(int)(Math.random()*200);
        plastic=(int)(Math.random()*200);
        density=(int)(Math.random()*200);
        pourosity=(float)(Math.random());
        
        
        
    }
    
    
    
    
    public void load_from(String name, int index) throws Exception {
        
        String[] z=loader(name);
        String b = z[index];
        
        string_load(b.split("PUT_SPACE_HERE"));
    }
    
    public String[] loader(String name) throws Exception {
        
        String[] whole_text=new String[0]; // create array to hold the text strings
        
        Path filePath = Paths.get(name); // set path
        Charset charset = StandardCharsets.UTF_8; // set the correct characterset
        //try {
            
            whole_text = (Files.readAllLines(filePath, charset)).toArray(new String[0]); // actually load the data into a single object
            
        //} catch (IOException ex) {  // if that fails, print I/O EXCEPTION
            //System.out.format("I/O Exception", ex);
        //}
        String running_total="";
        for(int i=0; i<whole_text.length; i++) {
            running_total+=whole_text[i]+"PUT_SPACE_HERE";
        }
        whole_text=running_total.split("&");
        
        return whole_text;
    }
    
    
    
    
    public void string_load(String[] data) throws Exception{// takes in correctly formated tags
        
        for(int i=0; i<data.length;i++) {
            data[i]=data[i].replaceAll("\\s", "");
            
            if(data[i].indexOf(':')>=0) {
                int z=data[i].indexOf(':');
                //System.out.println(data[i].substring(z+1,data[i].length()-1));
                switch (data[i].substring(1,z)) {
                case "melt_temp": melting_temp=Integer.parseInt(data[i].substring(z+1,data[i].length()-1)); //System.out.println("   melt");
                break;
                case "boil_temp": boiling_temp=Integer.parseInt(data[i].substring(z+1,data[i].length()-1)); //System.out.println("   boil");
                break;
                case "elastic": elastic=Integer.parseInt(data[i].substring(z+1,data[i].length()-1)); //System.out.println("   elastic");
                break;
                case "plastic": plastic=Integer.parseInt(data[i].substring(z+1,data[i].length()-1)); //System.out.println("   plastic");
                break;
                case "pourosity": pourosity=Float.parseFloat(data[i].substring(z+1,data[i].length()-1)); //System.out.println("   pourousity");
                break;
                case "density": density=Integer.parseInt(data[i].substring(z+1,data[i].length()-1)); //System.out.println("   density");
                break;
                case "composite": if(! data[i].substring(z+1,data[i].length()-1).equals("false")) { //if not false, this is a composite
                    String[] temp = data[i].substring(z+1,data[i].length()-1).split(",");
                    int[] composite_list= new int[temp.length];
                    for(int j=0; j< temp.length; j++) {
                        composite_list[j]=Integer.parseInt(temp[j]);
                    }
                    for(int j=0; j< composite_list.length; j++) {
                        material temp2=new material();
                        temp2.load_from("materials.txt",composite_list[j]);
                        //System.out.println(temp2);
                        //System.out.println(composite.length);
                        blend_material(temp2);
                        
                    }
                    
                }
                break;
                case "R":RGB[0] = (char)Integer.parseInt(data[i].substring(z+1,data[i].length()-1)); break;
                case "G":RGB[1] = (char)Integer.parseInt(data[i].substring(z+1,data[i].length()-1)); break;
                case "B":RGB[2] = (char)Integer.parseInt(data[i].substring(z+1,data[i].length()-1)); break;
                
                
                }
            }
        }
        
        
        
        
        
    }
    
    
}

这些都是重要的部分:

public class material {
    private int melting_temp=10200;
    private int boiling_temp=10500;
    private int elastic=10;
    private int plastic=10;
    private int density=10;
    private float pourosity=0; //how much liquid the material can absorb (0 - 1)
    private material[] composite= new material[1];  // what this meta-material is built out of. 
    private char[] RGB= {255,255,255};

    public void blend_material(material new_material) { // adds a new material to the composite
        
        //System.out.println(composite[0]);
        if(composite[0]==null) {
            composite[0]=new material(0,0,0,0,0,0);
            
        }
        composite = Arrays.copyOf(composite, composite.length+1);
        composite[composite.length-1]=new_material;
        
        melting_temp=0;
        boiling_temp=0;
        elastic=0;
        plastic=0;
        density=0;
        pourosity=0;
        for(int i=0; i<composite.length; i++) {  // set the properties of this meta-material to be the average of it's components
            melting_temp+=composite[i].get_melting_temp();
            boiling_temp+=composite[i].get_boiling_temp();
            elastic += composite[i].get_elastic();
            plastic += composite[i].get_plastic();
            density += composite[i].get_density();
            pourosity += composite[i].get_pourosity();
            for(int j=0; j<3;j++) {
                RGB[j]+=composite[i].get_RGB()[j];
            }
        }

            melting_temp/=composite.length-1;
            boiling_temp/=composite.length-1;
            elastic/=composite.length-1;
            plastic/=composite.length-1;
            density/=composite.length-1;
            pourosity/=composite.length-1;
            for(int j=0; j<3;j++) {
                RGB[j]=(char)Math.min(RGB[j]/composite.length,255);
            }
        
    }
}

现在,我只是试图在没有任何方法的情况下翻译基本类定义,但我似乎无法让composite变量工作。我不断收到关于类字段不完整的错误。这是问题代码:

 * Material.h
 *
 *  Created on: Aug 6, 2021
 *      Author: sting
 */
#include <memory>
using namespace std;


class Material {
public:
    Material(){

    }
    Material();
    virtual ~Material();

    //unsigned int temp;
    float dense=0;
    unsigned int plastic=0; // the threshold of when the material is permanently deformed.
    unsigned int breakage=0; // the threshold of force that breaks the material.
    float tConduct=0; // thermal conductivity
    float eConduct=0; // electrical conductivity

    unsigned int thickness=0; // the viscosity of the fluid at 300 degrees.
                            // NOTE: apparent viscosity changes as an exponential function. like 2^-x .
                            // for fluid flow simulations, the flow amount should be thickness^-300-x ticks per tile.
    unsigned int apparentThick=0;


    unsigned int melt=0; // the temperature at which the material melts
    unsigned int boil=0; // the temperature at which the material boils.

    struct Composite{

        shared_ptr<Material> material(new Material[]);
        unsigned char len=0;
        //bool init=false; // if this is true, the material is composed out of other materials.
    };
    Composite composite;

};

这个问题特别shared_ptr<Material> material(new Material[]);在行。我正在尝试建立一个系统,其中可以根据“元素”材料的平均值构建材料。

既然我显然做错了什么,我应该怎么做呢?

标签: javac++classrecursionmigration

解决方案


推荐阅读