阵列数据丢失了#39;将数组传递给另一个对象后

时间:2014-11-21 20:30:55

标签: c++ arrays object

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

宇宙物体:

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并为其提供数组的方法:

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...

我做错了什么?

1 个答案:

答案 0 :(得分:2)

首先,您的代码无效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) {}
};

请注意用std::vector替换非C ++代码。另请注意,我们使用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的调用之后,您看到向量没有传递正确的信息,那么请进一步查看。上面的代码符合&#34; normal&#34; C ++,这样我们就可以进一步找出问题。