2016-05-28 58 views
-1
#include "solarsystem.h" 
#include "planet.h" 
#include <vector> 

static int number_of_planets, planet_type; 
String planet; 
String planet_id; 
static std::vector<Planet> allPlanets(number_of_planets); 

void Solarsystem::planets() 
{ 
    allPlanets.size(); 

    srand(time(NULL)); 

    number_of_planets = rand() % 8 + 1; 

    for (int i = 0; i <= number_of_planets; i++) 
    { 
     srand(time(NULL)); 

     planet_type = rand() % 100 + 1; 

     if (i <= 2) 
     { 
      if (planet_type >= 0) 
      { 
       planet_id = "barren"; 
      } 

      if (planet_type >= 31) 
      { 
       planet_id = "gas"; 
      } 

      if (planet_type >= 71) 
      { 
       planet_id = "desert"; 
      } 
     } 

     else if (i <= 5) 
     { 
      if (planet_type >= 0) 
      { 
       planet_id = "barren"; 
      } 

      if (planet_type >= 21) 
      { 
       planet_id = "gas"; 
      } 

      if (planet_type >= 41) 
      { 
       planet_id = "ocean"; 
      } 

      if (planet_type >= 61) 
      { 
       planet_id = "continental"; 
      } 

      if (planet_type >= 81) 
      { 
       planet_id = "jungle"; 
      } 
     } 

     else if (i <= 8) 
     { 
      if (planet_type >= 0) 
      { 
       planet_id = "barren"; 
      } 

      if (planet_type >= 31) 
      { 
       planet_id = "gas"; 
      } 

      if (planet_type >= 71) 
      { 
       planet_id = "ice"; 
      } 
     } 

     allPlanets[i] = Planet(); 
    } 
} 

void Solarsystem::render(RenderWindow &window) 
{ 
    if (inside == true) 
    { 
     if (a == true) 
     { 
      planets(); 

      a = false; 
     } 

     planet = planet_id; 

     for (int z = 1; z <= number_of_planets; z++) 
     { 
      int x = 700; 
      int y = 100; 

      if (z == 1) 
      { 
       allPlanets[z].planet_spr.setOrigin(700/2, 700/2); 
       allPlanets[z].planet_spr.setPosition(0, 0); 
      } 

      else 
      { 
       allPlanets[z].planet_spr.setOrigin(275/2, 275/2); 
       allPlanets[z].planet_spr.setPosition(x, y); 

       int x = x + 700; 
       int y = y + 100; 
      } 

      allPlanets[z].render(window, planet); 
     } 
    } 

    else if(inside == false) 
    { 
     solarsystem_txt.loadFromFile("solarsystem.png"); 
     solarsystem_spr.setTexture(solarsystem_txt); 

     window.draw(solarsystem_spr); 
    } 
} 

你好,我有一個問題,每次我想開始我的PROGRAMM我得到的錯誤:「表達式:向量下標越界」C++(SFML)向量下標越界

錯誤是lockated在這裏:

 if (size() <= _Pos) 
     { // report error 
     _DEBUG_ERROR("vector subscript out of range"); 
     _SCL_SECURE_OUT_OF_RANGE; 
     } 

(不是我的代碼)

我只是用矢量開始,我不知道我做錯了什麼。

+0

您正試圖訪問比您的矢量大的索引。檢查你在看'allPlanets [z]'也許。另外,查看錯誤的堆棧跟蹤,然後回到導致問題的代碼行。它也可能值得一看像'std :: map',用於將你的行星串id映射到整數ID。 – danielunderwood

+0

這個'for(int i = 0; i <= number_of_planets; i ++)'應該是for(int i = 0; i Galik

+0

感謝您的幫助。我會查找std :: map – user6395356

回答

-1

更新:

「for」有問題。該代碼的作品,直到「std :: cout < planet_type < < std :: endl;」,該程序一次性在控制檯中寫入planet_type,但不是「ok」,所以它必須是「allPlanets [i] = Planet ();」

#include "solarsystem.h" 
#include "planet.h" 
#include <vector> 

static int number_of_planets, planet_type; 
String planet; 
String planet_id; 
static std::vector<Planet> allPlanets; 

void Solarsystem::planets() 
{ 
    srand(time(NULL)); 

    number_of_planets = rand() % 8 + 1; 

    std::cout << number_of_planets << std::endl; 

    for (int i = 1; i <= number_of_planets; i++) 
    { 
     srand(time(NULL)); 

     planet_type = rand() % 100 + 1; 

     if (i <= 2) 
     { 
      if (planet_type >= 0) 
      { 
       planet_id = "barren"; 
      } 

      if (planet_type >= 31) 
      { 
       planet_id = "gas"; 
      } 

      if (planet_type >= 71) 
      { 
       planet_id = "desert"; 
      } 
     } 

     else if (i <= 5) 
     { 
      if (planet_type >= 0) 
      { 
       planet_id = "barren"; 
      } 

      if (planet_type >= 21) 
      { 
       planet_id = "gas"; 
      } 

      if (planet_type >= 41) 
      { 
       planet_id = "ocean"; 
      } 

      if (planet_type >= 61) 
      { 
       planet_id = "continental"; 
      } 

      if (planet_type >= 81) 
      { 
       planet_id = "jungle"; 
      } 
     } 

     else if (i <= 8) 
     { 
      if (planet_type >= 0) 
      { 
       planet_id = "barren"; 
      } 

      if (planet_type >= 31) 
      { 
       planet_id = "gas"; 
      } 

      if (planet_type >= 71) 
      { 
       planet_id = "ice"; 
      } 
     } 

     std::cout << planet_type << std::endl; 

     allPlanets[i] = Planet(); 

     std::cout << "ok" << std::endl; 
    } 
} 
+0

不是一個答案,請編輯該問題。 – Galik

0

number_of_planets的初始值是0,所以矢量allPlanets的初始大小也爲0的矢量的大小沒有改變,所以通過allPlanets[i]所有訪問都出界。