C++ 数组数据为';丢失';将数组传递给另一个对象后

C++ 数组数据为';丢失';将数组传递给另一个对象后,c++,arrays,object,C++,Arrays,Object,当我通过构造函数传递数组时,数组中的对象丢失了。我的第一个猜测是,我需要将它更改为一个指针数组,但这导致了一个segfault。我的下一个猜测是,我需要在传递数组数据后复制它,但这也不起作用。以下是问题代码: 宇宙对象: class Universe { public: Star stars[]; int starsLength; Planet planets[]; int planetsLength; public: Universe(Star st[], int

当我通过构造函数传递数组时,数组中的对象丢失了。我的第一个猜测是,我需要将它更改为一个指针数组,但这导致了一个segfault。我的下一个猜测是,我需要在传递数组数据后复制它,但这也不起作用。以下是问题代码:

宇宙对象:

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

<>我做了什么?

首先,你的代码不是有效的C++。在C++中不存在使用<代码> []/COD>声明空数组的问题。p> 首先,把它变成有效的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) {}
};
可以替换为

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

代码的其余部分保持不变。现在,如果在调用创建
宇宙
之后,您看到向量没有传递正确的信息,请进一步查看。上面的代码符合“正常”C++,这样我们就可以进一步解决这个问题。< /P>你是通过拷贝而不是引用来传递数组吗?<代码>星星> st /代码>应该做什么?编译?(特别是
宇宙的第一和第三成员
…)不在科里鲁:@newObjekt-这是什么:
恒星[]
?这不是C++,你应该看看使用。谢谢!星期一我一定要试一试。是否有充分的理由在类似于向量的库上使用标准数组类型?标准数组看起来非常有限。C++标准数组大小固定,而向量不是固定大小的(它可以在没有任何内存管理的情况下调整大小)。即使您使用的是固定大小的数组,
std::array
也是一个更好的选择。
#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) {}
};
  for(int i = 0;i < starsLength;i++) {
  for(int i = 0;i < stars.size();i++) {
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);