Debug Assertion Failed - Array Iterators Incompatible

745 Views Asked by At

I am trying to get the intersection of an array and a vector. I decided to make them both vectors because it's how cplusplus.com gives an example.

This is the error message I get:

enter image description here

and here is my code:

enter image description here

the getNumbers() function returns an array.

EDIT

Here is the getNumbers() function - it just returns a property of the same return type:

enter image description here

1

There are 1 best solutions below

3
On

This assertion usually means that the iterators supplied as the beginning and the end of a range reference different collections.

the getNumbers() function returns an array.

Unless getNumbers() returns an array by reference, and also returns the reference to the same array, this initialization is invalid:

std::vector<unsigned int> ticketNumbers(getNumbers().begin(), getNumbers().end());

In order for the above to work, getNumbers() must repeatedly return a reference to the same array. Your getNumbers returns a copy, because it returns an array by value.

To fix this line, first call getNumbers(), store the result in a temporary variable tempNumbers, like this

std::array<unsigned int, TICKET_BALL_COUNT> tempNumbers= getNumbers();
std::vector<unsigned int> ticketNumbers(tempNumbers.begin(), tempNumbers.end());

Alternatively, you could change your getNumbers() function to return a const reference, like this:

const std::array<unsigned int, TICKET_BALL_COUNT>& Ticket::getNumbers() const {
    return _numbers;
}