c++ - Vector Subscript out of Range During Projectile Creation -


there 2 microsoft visual c++ runtime library messages pop when attempt create projectiles while game running

debug assertion failed! program: c:\windows\system32\msvcp120d.dll file: c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector line:1201 expression: vector subscript out of range 

and

debug assertion failed! program: c:\sfml-2.3.2\sfml\debug\sfml.exe file: c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector line:1202 expression: "standard c++ libraries out of range" && 0 

and message microsoft visual studio express 2013:

unhandled exception @ 0x5b89a893 (msvcr120d.dll) in sfml.exe: invalid parameter passed function considers invalid parameters fatal.

this occurs press space bar create projectile (green square). green square appears on screen should , message pops up. projectile @ point current code should behave green square paint brush, message pops up. here source code:

main:

int main ( )  { int counter1 = 0; int counter2 = 0; int counter3 = 0;  //create main window  sf::renderwindow window(sf::videomode(640, 480), "sfml demo");  sf::event event;  sf::vector2i source(1, 0);  //load texture  sf::texture textureplayer;  if (!textureplayer.loadfromfile("images/player.png"))     return exit_failure;  //class object class player player1; player1.sprite.settexture(textureplayer);  //projectile vector array vector<projectile>::const_iterator iter; vector<projectile> projectilearray;  //projectile object  class projectile projectile1;   while ( window.isopen() ) {     while (window.pollevent(event))     {         if ( event.type == sf::event::eventtype::closed )             window.close();     }     window.clear();      //fire missile (space bar)     if (sf::keyboard::iskeypressed(sf::keyboard::space))     {         projectile1.rect.setposition(player1.rect.getposition());         projectilearray.push_back(projectile1);     }      //draw projectile     (iter = projectilearray.begin(); iter != projectilearray.end(); iter++)     {         window.draw(projectilearray[counter1].rect);         counter1++;     }       //update player rectangle collision detection     player1.update();       player1.updatemovement();       //window.draw(player1.rect);     window.draw(player1.sprite);     window.display();    }  } 

projectile.h

class projectile: public entity //inheriting entity { public:

float movementspeed = 10; int attackdamage = 5;  projectile();               //constructor //void update(); //void updatemovement(); }; 

#endif

projectile.cpp

    projectile::projectile() {  rect.setsize(sf::vector2f(10.0, 10.0));  rect.setfillcolor(sf::color::green);  rect.setposition(0, 0);  //sprite.settexturerect(sf::intrect(0, 0, 32, 32)); } 

how fix this? advice/help appreciated. in advance

in cycle draw projectile must use or iterator access elements in projectilearray this

for (iter = projectilearray.begin(); iter != projectilearray.end(); iter++) {     window.draw(iter->rect); } 

or initialize counter1=0; before cycle otherwise in second iteration of main while counter1 not point on begin of vector projectilearray.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -