Jump to content
  • Advertisement
Sign in to follow this  
Elit3d

[SFML] [C++] vector subscript out of range

This topic is 1042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

	//Enemy projectile collide

	collideCounter = 0;
	
	for (iter2 = projArray.begin(); iter2 != projArray.end(); iter2++){
		collideCounter2 = 0;

		for (iter3 = projArray.begin(); iter3 != projArray.end(); iter3++){
			if (projArray[collideCounter].rect.getGlobalBounds().intersects(enemy.enemyArray[collideCounter2].enemySprite.getGlobalBounds()))
			{
				std::cout << "COLLIDE" << std::endl;
			}

			collideCounter2++;
		}
		collideCounter++;
	}

Seems to be the 2nd for loop giving me the subscript out of range. I have no idea where to start looking, it could be the amount of bullets that are being spawned because if I click my mouse fast, it will only spawn a few bullets instead of like 8-10 so maybe its conflicting with the collision but I'm not sure, only this part of the code is giving me the error.

Share this post


Link to post
Share on other sites
Advertisement
On your specific error, probably you are expecting to iterate over enemy.enemyArray for the inner loop rather than projArray.

If that was a typo in your post but different in your actual code, stop that in the debugger when the error occurs and look at the values of collideCounter and collideCounter2. You should find that one of them exceeds the length of the array they are being used on, either projArray or enemyArray.


As for the general condition, I sure hope you have a small number of items in those arrays. That kind of all-to-all comparison results in dangerously fast computational growth. Most systems build spatial trees and only compare the immediate neighbors.

Share this post


Link to post
Share on other sites

On your specific error, probably you are expecting to iterate over enemy.enemyArray for the inner loop rather than projArray.

If that was a typo in your post but different in your actual code, stop that in the debugger when the error occurs and look at the values of collideCounter and collideCounter2. You should find that one of them exceeds the length of the array they are being used on, either projArray or enemyArray.


As for the general condition, I sure hope you have a small number of items in those arrays. That kind of all-to-all comparison results in dangerously fast computational growth. Most systems build spatial trees and only compare the immediate neighbors.

wow thanks a lot! can't believe I was overlooking that this whole time

Share this post


Link to post
Share on other sites
Why are you using iterators in your for loops, in addition to counters? Just dereference the iterator...

Or better yet, if your compiler supports C++11, use the new range-for construct which eliminates these kinds of issues entirely smile.png

for (const auto& item : myArray)
{
  // do some calculation with item - which is basically "myArray[i]"
}

Share this post


Link to post
Share on other sites
I don't have experience with this:
 

for (const auto& item : myArray)
{
  // do some calculation with item - which is basically "myArray[i]"
}


But...

Why are you using iterators in your for loops, in addition to counters? Just dereference the iterator...


	//Enemy projectile collide
	
	for (iter2 = projArray.begin(); iter2 != projArray.end(); ++iter2){

		for (iter3 = enemy.enemyArray.begin(); iter3 != enemy.enemyArray.end(); ++iter3){
			if (*iter2.rect.getGlobalBounds().intersects(*iter3.enemySprite.getGlobalBounds()))
			{
				std::cout << "COLLIDE" << std::endl;
			}

		}
	}
Edited by MarkS

Share this post


Link to post
Share on other sites

I don't have experience with this:





for (const auto& item : myArray)
{
  // do some calculation with item - which is basically "myArray[i]"
}


This code basically is saying "for each item in myArray" - where "item" is a const ref of whatever the element type of myArray is.

It is the functional equivalent of:
const auto endIter = end(myArray);
for (auto iter = begin(myArray); iter != endIter; ++iter)
{
  const auto& item = *endIter;
  // do stuff here
}
"begin" and "end" are non-member functions that call "begin" and "end" as members on containers, but are non-members themselves so they can be specialized for compile-time arrays so you can use the ranged-for syntax on them:

int myArray[20];
// fill myArray
for (const auto& item : myArray)
{
  // do stuff with the items
}
This won't work, because the compiler has no way to get the size of the array:
int* myArray = new int[size];
// fill myArray
for (const auto& item : myArray) // compile error!
{
  // do stuff with the items
}
delete [] myArray;
Which is one reason it is typically preferred to use std::array and std::vector over raw C arrays.

Share this post


Link to post
Share on other sites

Thanks. Since I don't have experience with it, I couldn't use it as an example. It appears to be the better way though.

Share this post


Link to post
Share on other sites

Why are you using iterators in your for loops, in addition to counters? Just dereference the iterator...

 

Or simpler still, iterators can be used like this....

	if(someIterator.hasNext()){
	     while(someIterator.hasNext()){
		  functionDoingSomeStuff( someIterator.next(), someIterator.previousIndex()  );// Parameters:get object referenced with its index
	    }
        }

Though in Java i expect the principle to be applicable in C++

Share this post


Link to post
Share on other sites

Though in Java i expect the principle to be applicable in C++

 

Nope. There does not exist anything like hasNext or conversion between iterators and indexes (previousIndex) in C++

Edited by Olof Hedman

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!