2014-11-21 76 views
-5

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

Universe对象:

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

我究竟做错了什么?

+1

难道你是通过拷贝而不是通过引用传递数组吗? – 2014-11-21 20:32:22

+0

听起来可能是这个问题。参考是&符号的权利?我不确定如何使用它。 – 2014-11-21 20:35:18

+1

什么是“星星”应该做的? – 2014-11-21 20:35:32

回答

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

注更换非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++,这样我们可以进一步找出问题。

+0

谢谢!我肯定会在周一发布这个消息。有没有很好的理由在像vector这样的库上使用标准数组类型?标准阵列似乎非常有限。 – 2014-11-21 21:26:33

+0

C++标准数组的大小是固定的,而矢量的大小不固定(可以在不进行任何内存管理的情况下调整大小)。即使你使用的是固定大小的数组,'std :: array'也是更好的选择。 – PaulMcKenzie 2014-11-21 23:07:01