2016-05-28 3 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); 
    } 
} 

Здравствуйте У меня есть проблема, каждый раз, когда я пытаюсь начать свою программку я получаю ошибку: «Выражение: вектор индекс вне диапазона»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' для сопоставления идентификаторов вашей планеты с целыми идентификаторами. – danielunderwood

+0

Это 'for (int i = 0; i <= number_of_planets; i ++)' должно быть 'for (int i = 0; i Galik

+0

Спасибо за помощь. Я посмотрю std :: map – user6395356

ответ

-1

Update:

Это то, что случилось с "за". Код работает до тех пор, пока не будет «std :: cout < planet_type < < std :: endl;», программа пишет planet_type в консоли один раз, но не «хорошо», поэтому это должно быть что-то с «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] выходят за границы.

Смежные вопросы