Hello, first off thanks for reading.

So my problem occurs when I try to copy an element from one vector into a second vector.
The error that I get says that the vector subscript is out of range when I try to push_back that element.
I only get an error when there is more than one face detected (element in the DetectedFaces vector), if there's only one element in the vector, it works fine.

while(!DetectedFaces.empty())
{
	temp.push_back(DetectedFaces[0]);  /////bad line
	DetectedFaces.erase(DetectedFaces.begin());
}

Any ideas what would be wrong with this? or is there any other way to implement this so that all elements from the DetectedFaces vector get added onto the end of the temp vector?

Thanks again.

I see nothing technically wrong with the original code, which suggests that the problem is elsewhere.

However, the code is unnecessarily complicated and can also be very slow if the DetectedFaces vector is large. Moreover, it destroys the DetectedFaces vector, which may or may not be what you want.

A much easier solution:

temp.insert(temp.end(), DetectedFaces.begin(), DetectedFaces.end());

Thanks for replying guys,
Yeah, I agree that my while-loop is unnecessarily complicated.
I had the a for-loop like the first first reply, but that wasn't working. I was thinking that maybe my indexing was going outside of the number of elements, so I changed it to only access the 0th element because that should always be there, but I hit the same problem either way.

I also tried inserting all the faces at once, but I hit the same error.

The error that I get while running it refers me to this:

if (size() <= _Pos)
{	// report error
     _DEBUG_ERROR("vector subscript out of range");
                    ////error told me to refer the above line.
     _SCL_SECURE_OUT_OF_RANGE;
}

It doesn't tell me which line causes the error but I limited it down to the original problem line (line 3 in my original post), whether I use the for-loop, while-loop, or the temp.insert line when I tried that outside of the loop.
And with all of the attempts, it's the same scenario: there's only an error if the number of faces in the DetectedFaces vector is over one.

They are both vectors with elements of a class that I wrote called 'face' :

class face
{
public:
	face();
	face(Point FaceCenter, int FaceRadius);
	
	Point FaceCenter;	//// (FaceCenter.x, FaceCenter.y)
	int FaceRadius;		//// Radius of face
	double Dist_Point;	
	clock_t FOV_start_time; // Beginning of time when face appearred in the Field of Vision(=clock())
	double Time_in_FOV;     // Time that a face has been in the FOV (clock() - FOV_start_time)
};

I changes the function to avoid pushing back values, but I get the same error still.
So as of right now, it looks like this (my function is type void and has no parameters):

void function()
int flag; //0 if face not detected in TrackingFaces -- 1 if face was within the threshold of a face in TrackingFaces
for(int i=0; i < DetectedFaces.size(); i++)
{
	flag=0;
	for(int j=0; j<TrackingFaces.size(); j++)
	{
		if( THRESHOLD)
                /* this if statement is complicated, but I know it works. Basically it checks to see the DetectedFaces[i] is the same face as TrackingFace[j]*/
		{
                //if it is the same face then I change some of the data for the face
			DetectedFaces[i].Time_in_FOV = double(clock() - DetectedFaces[j].FOV_start_time)/CLOCKS_PER_SEC;
			flag =1; // this flag lets me know that I have found the face that matches
		}
	}
	if(flag==1)// if I haven't found a matching face, then it's a new face.
	{
		DetectedFaces[i].FOV_start_time =clock();
		DetectedFaces[i].Time_in_FOV=0;
	}
}
}

I get errors when there are multiple faces

In the last section of code you posted, you are referring to DetectedFaces[j]. If TrackingFaces has more elements than DetectedFaces, at some point j will be >= DetectedFaces.size(). When that happens, DetectedFaces[j] is an out-of-range subscript.

Alright! It's working beautifully!

Funny thing is I noticed the 'j' in the first half of that statement, realized it wouldn't work and changed it back to 'i', but I guess I ignored the second half. I'm embarrassed..

Thank you so much, It probably would've taken me a looong time to find that.

This question has already been answered. Start a new discussion instead.