14 Replies - 557 Views - Last Post: 01 May 2013 - 03:35 PM Rate Topic: -----

#1 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

vector subscript out of range

Posted 01 May 2013 - 04:12 AM

i was making a space shooter type of game and in the part of the code that kills enemys when hit by player bullets i get a runtime error mostly after i have killed 3-4 enemy's
using visual c++ 2008 with sfml
the code for the function is
void killenemy(std::vector<bullet*> &vb,std::vector<enemy*> &ve)
{for(int i=0 ; i<vb.size();i++)
  {for(int j=0; j<ve.size();j++)
   {if(vb[i]->getrect().intersects(ve[i]->getrect()))
    { bullet *b1=vb[i];
      vb.erase(vb.begin()+i);
	  delete b1;
	  ve[j]->life-=10;
      if(ve[j]->life<=0)
	  {enemy *e1=ve[j];
	  ve.erase(ve.begin()+j);
	  delete e1;
	  }
    }
  }
 }
}


getrect is a function in bullet as well as enemy class which returns the globalbounds and intersect checks for intersection

the function is called as here
killenemy(player.bullets,wave);


where player.bullets is a vector in the player class of pointers to bullet objects
and wave is a global vector of pointer to enemy objects

i know that because when i dont include this function in my code everything runs smoothly
plzz help

This post has been edited by B0rn2c0de: 01 May 2013 - 04:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: vector subscript out of range

#2 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: vector subscript out of range

Posted 01 May 2013 - 04:27 AM

This error means that your std::vector has not got the object that your function is trying to grab. So if I have a vector like so:
std::vector< unsigned int > myVector;

myVector.push_back(10);
myVector.push_back(20);
myVector.push_back(30);
myVector.push_back(40);



And I try this:
std::cout << myVector[4] << std::endl;



I will receive a vector Subscript out of range error. Try output the size of the vector you are trying to access:
std::cout << myVector.size() << std::endl;



Also, you should use std::vector<type>::iterator to iterate over your objects. I found an article here on std::vector iterators:
http://www.dreaminco...ctor-iterators/
http://www.cprogramm.../iterators.html

Also, don't forget, array subscripts begin at 0 and end at n-1, where n is the array size. So if I have a vector with 5 objects in it, I would access the last object at myVector[myVector.size()-1].
Hope this helps :)/>

This post has been edited by aaron1178: 01 May 2013 - 04:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

Re: vector subscript out of range

Posted 01 May 2013 - 05:44 AM

nopes still getting the same error :(
Was This Post Helpful? 0
  • +
  • -

#4 yothsoggoth  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 49
  • Joined: 19-June 12

Re: vector subscript out of range

Posted 01 May 2013 - 06:59 AM

What have you tried? aaron1178's post gave quite a bit of guidance. You've said your code still doesn't work, but you haven't said which part of his guidance you took.

Also, to be a little picky, if you tidy your code up it may help. Get some comments in there and neaten it up.
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - Not Quite Known
Pick a style guide and stick to it, mainly for readability and consistency. I like this one: http://www.possibili...ngStandard.html


Post what you've tried, and we can try again to help you.
Was This Post Helpful? 0
  • +
  • -

#5 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: vector subscript out of range

Posted 01 May 2013 - 08:32 AM

You do realize that this is very dangerous:

bullet *b1=vb[i];



then

delete b1;


Do not do this! You are removing the memory pointing to your vector vb and corrupting the vector itself.

The code in your snippet is generally bad, as in dangerous. You need to decide if you are using vectors or simple pointers, you can't mix their uses safely.
Was This Post Helpful? 0
  • +
  • -

#6 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

Re: vector subscript out of range

Posted 01 May 2013 - 09:01 AM

is that the reason it isnt working?? .. i couldnt think of any other way of doing this ..is there a better way ?? :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#7 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: vector subscript out of range

Posted 01 May 2013 - 10:46 AM

View PostButchDean, on 01 May 2013 - 12:32 PM, said:

You need to decide if you are using vectors or simple pointers, you can't mix their uses safely.

Did you read this bit?
Was This Post Helpful? 0
  • +
  • -

#8 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

Re: vector subscript out of range

Posted 01 May 2013 - 11:12 AM

Yes i did .. But i dont know how to spawn enemies without using a vector of pointers
My spawn code is something like this
Void spawn(sf::clock x)
{
If(x.getelapsedtime().asmilliseconds()%1000 && wave.size()<wave.capacity())
{ wave.push_back(new enemy);
}

Then i update their movements in another function by iterating through the the vector ..
If i dont wanna use vectors with pointers what else can i use ??
Was This Post Helpful? 0
  • +
  • -

#9 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: vector subscript out of range

Posted 01 May 2013 - 11:36 AM

I personally would use a linked list (which would involve pointers), simply because it is faster than using vectors. Vectors keep things simple at the cost of speed.

What is If(x.getelapsedtime().asmilliseconds() bit supposed to mean? That will always be true, right?
Was This Post Helpful? 0
  • +
  • -

#10 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

Re: vector subscript out of range

Posted 01 May 2013 - 01:38 PM

okay so i followed your way and first of all converted the bullets vector into a list but i dunno why now i cannot even see the bullets when i press space there is just a blue flash and gone

if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
          {once = true;}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
	{
        sf::Vector2f vp=ss.getPosition();
	if(bullets.size()<10&&once==true)
	{bullets.push_back(new bullet(vp));}
	once=false;}

what it does is pushes back a new bullet object and sets its initial position at the top of the ship

and here is my update code .. i call this in the game loop it updates the position of bullet object
void ship::updatebullets(sf::RenderWindow &kame)
{std::list<bullet*>::iterator it;
    for(it=bullets.begin();it!=bullets.end();)/>
	{ if((*it)->move(kame)==true) // returns true when the bullet goes out of screen
	  { delete *it;
	    it=bullets.erase(it);
	   	    }
	}
}

previously when i was using vectors this code was as given and it worked perfectly
void ship::updatebullets(sf::RenderWindow &kame)
{ 	for(int i=0;i<bullets.size();i++)
	{ if(bullets[i]->move(kame)==true)
	  { bullet *b1= bullets[i];
	    bullets.erase(bullets.begin() +i);
	   delete b1;
	    }
	}
}
	


now there are only blue flashes not actual bullets .. at first i thought the speed must be to high so i lowered the speed in move function to 0.001 still the same i really dont get it :(/>

Quote

What is If(x.getelapsedtime().asmilliseconds() bit supposed to mean? That will always be true, right?


actually the full line is
If(x.getelapsedtime().asmilliseconds()%1000 ==0 && wave.size()<wave.capacity())

which returns true only every thousand miliseconds or else the enemy all spawn altogether

This post has been edited by B0rn2c0de: 01 May 2013 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -

#11 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: vector subscript out of range

Posted 01 May 2013 - 02:06 PM

No, I didn't say 'list' I said 'linked list'.
Was This Post Helpful? 0
  • +
  • -

#12 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

Re: vector subscript out of range

Posted 01 May 2013 - 03:02 PM

But the list in the stl is itself adoubly linked. List
Was This Post Helpful? 0
  • +
  • -

#13 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: vector subscript out of range

Posted 01 May 2013 - 03:13 PM

My point is to not use anything from the STL in your game, yes STL list is a doubly linked list but it comes with a lot of bloat that reduces speed and memory efficiency.

Writing your own will be a lot lighter and quicker.
Was This Post Helpful? 0
  • +
  • -

#14 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

Re: vector subscript out of range

Posted 01 May 2013 - 03:18 PM

Okay so u r saying that i write my own template class ?? .. But why dosent it work with the stl ones .. And the flashing bullet thing occurs when i use iterators with lists or with vectors dosent matter but when i use vectors with [] operator it works properly why is that so ??
Was This Post Helpful? 0
  • +
  • -

#15 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: vector subscript out of range

Posted 01 May 2013 - 03:35 PM

Again, I didn't say template class, I said "linked list". Everywhere is littered with a variety of tutorials on them, I'm not going through the details.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1