数组数据在将数组传递给另一个对象后'lost'

Array data is 'lost' after passing the array to another object

本文关键字:数组 lost 一个对象 数据      更新时间:2023-10-16

>我遇到了一个问题,当我通过构造函数传递数组时,数组中的对象丢失了。我的第一个猜测是我需要将其更改为指针数组,但这导致了段错误。我的下一个猜测是我需要在传递数组数据后复制它,但这也没有用。这是问题代码:

宇宙对象:

class Universe {
public: 
    Star stars[]; int starsLength;
    Planet planets[]; int planetsLength;
public: 
    Universe(Star st[], int stl, Planet pl[], int pll) {
        stars < st; starsLength = stl;
        planets < pl; planetsLength = pll;
    }
    Universe() {
    }
public:
    void render() {        
        for(int i = 0;i < starsLength;i++) {
            //std::cout << "STAR: " << stars[i].location.x << "," << stars[i].location.y << " " << stars[i].size << " " << stars[i].color.r << "," << stars[i].color.g << "," << stars[i].color.b << "n";
            renderCircle(stars[i].location, stars[i].size, stars[i].color);
        }
        for(int i = 0;i < planetsLength;i++) {
            renderCircle(planets[i].location, planets[i].size, planets[i].color);
        }
    }
    void renderCircle(Point location, float size, Color color) {
       glBegin(GL_LINES);
            glColor3f(color.r,color.g,color.b);
            glVertex2f(location.x+size, location.y+size);
            glVertex2f(location.x-size, location.y-size);
            glVertex2f(location.x-size, location.y+size);
            glVertex2f(location.x+size, location.y-size);
       glEnd();
    }
};

创建宇宙并为其提供数组的方法:

Universe buildUniverse(int size, int seed) {
    Point bounds = Point{static_cast <float> (size),static_cast <float> (size)}; //0,0 to size,size
    int starCount = min(size/10,random(size/5));
    int planetCount = min(size/3,random(size));
    Star stars[starCount];
    Planet planets[planetCount];
    //std::cout << "-- Created " << starCount << " stars and " << planetCount << " planets...n";
    for(int i = 0;i < starCount;i++) {
        Point location = {random(bounds.x),random(bounds.y)};
        Point velocity = {random(bounds.x/100.0f),random(bounds.y/100.0f)};
        float size = random(bounds.x/100.0f);
        float mass = random(size*(random(1.0f)+0.5f));
        Color color = {1.0f,1.0f,1.0f};
        stars[i].setStar(location,velocity,size,mass,color);
    }
    for(int i = 0;i < planetCount;i++) {
        Point location = {random(bounds.x),random(bounds.y)};
        Point velocity = {random(bounds.x/100.0f),random(bounds.y/100.0f)};
        float size = random(bounds.x/100.0f);
        float mass = random(size*(random(1.0f)+0.5f));
        Color color = {random(1.0f),random(1.0f),random(1.0f)};
        planets[i].setPlanet(location,velocity,size,mass,color);
    }
    Universe uni = {stars, starCount, planets, planetCount};
    std::cout << "Star in array: " << stars[0].location.x << "," << stars[0].location.y << " " << stars[0].size << " " << stars[0].color.r << "," << stars[0].color.g << "," << stars[0].color.b << "n";
    std::cout << "Star passed to uni in an array: " << uni.stars[0].location.x << "," << uni.stars[0].location.y << " " << uni.stars[0].size << " " << uni.stars[0].color.r << "," << uni.stars[0].color.g << "," << uni.stars[0].color.b << "n";
    return uni;
}

程序的输出:

Building universe...
Star in array: 39.922,39.155 0.167611 1,1,8.85715e-39
Star passed to uni in an array: 7.00649e-45,2.24208e-44 0.0282954 5.90446e-39,1.4013e-45,1.4013e-45
Initializing threaded renderer...
Starting simulation...

我做错了什么?

首先,您的代码C++无效。 使用 [] 声明空数组在 C++ 中不存在。

因此,第一件事是将其转换为有效的C++,仍然保留您要完成的任务。 一种解决方案是使用std::vector

#include <vector>
class Universe {
public: 
    std::vector<Star> stars;
    std::vector<Planet> planets;
public: 
    Universe(const std::vector<Star>& st, 
             const std::vector<Planet>& pl) : stars(st), planets(pl) {}
};

请注意将非C++代码替换为 std::vector 。 另请注意,我们使用 initializer-list 初始化向量。

最后,请注意,我们不再需要将大小保留为单独的成员变量。 为什么? 因为向量通过调用 vector::size() 成员函数知道它的大小。 例如:

  for(int i = 0;i < starsLength;i++) {

可以替换为

  for(int i = 0;i < stars.size();i++) {

buildUniverse函数中,使用以下更改:

Universe buildUniverse(int size, int seed) {
    Point bounds = Point{static_cast <float> (size),static_cast <float> (size)}; //0,0 to size,size
    int starCount = min(size/10,random(size/5));
    int planetCount = min(size/3,random(size));
    std::vector<Star> stars(starCount);
    std::vector<Planet> planets(planetCount);
    //...
    Universe uni(stars, planets);

代码的其余部分保持不变。 现在,如果在调用创建Universe之后,您看到向量没有传递正确的信息,请进一步查看。 上面的代码符合"正常"C++,因此我们可以进一步找出问题所在。