2014-11-21 201 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

相關問題