Errors: Division by zero and iterator not dereferencable

Asked

Viewed 25 times

-1

I’m having trouble compiling this code, it returns me two errors while running it:

vector<bool> cp( size );

// determine crossover point (randomly)
for( int i = _numberOfCrossoverPoints; i > 0; i-- )
{
    while( 1 )
    {
        int p = rand() % size;
        if( !cp[ p ] )
        {
            cp[ p ] = true;
            break;
        }
    }
}

the first error is related to the definition of p, it says that it occurs divided by zero.

the second error is related to trying to access cp with such p, it says iterator not dereferencable.

I wanted to understand why these errors are returned and how to fix them.

1 answer

0

The error of division by zero in int p = rand() % size; will occur if the variable "size" is zero. The second error is probably the result of the first error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.