Search code examples
c++opencvstdout

std::out_of_range error


I am working on the following code in opencv in Linux Ubuntu. x_captured and y_captured are "int" type vectors. Size of both vectors is 18.

for (int i=0;i<=x_captured.size();i++)
{
    for (int j=0;j<=y_captured.size();j++)
    {
        if (i!=j)
        {
            if (((x_captured.at(j)-x_captured.at(i))<=2) && 
                ((y_captured.at(j)-y_captured.at(i))<=2))
            {
                consecutive=consecutive+1;
            }
        }
    }
}

But when i=0 and j=18 after that it throws the following error:

terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check


Solution

  • for (int i=0;i<=x_captured.size();i++)
            {
                for (int j=0;j<=y_captured.size();j++)
    

    You should change the <= to < and try again.

    enter image description here

    Example array named Billy : Size : 5 but last index is 4. Get it? :)