3

I'm having some trouble compiling code that uses std::lower_bound on systems with newer versions of gcc. From my tests, 4.1 works, but 4.6 fails.

Here is the relevant code snippet:

template <typename RandomAccessIterator, typename Value, typename Comparer>
int my_binary_search(RandomAccessIterator const  first, RandomAccessIterator const  last, Value const& value, Comparer comparer)
{
    RandomAccessIterator it(std::lower_bound(first, last, value, comparer));
    //some more code below
}

The error I am seeing is:

error: no matching function for call to ‘lower_bound(const __gnu_cxx::__normal_iterator<int*, std::vector<int> >&, const __gnu_cxx::__normal_iterator<int*, std::vector<int> >&, const int&, bool (*&)(int, int))’
testing.h:37:75: note: candidate is:
/usr/include/c++/4.6/bits/stl_algobase.h:936:5: note: template<class _ForwardIterator, class _Tp> _ForwardIterator std::lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&)

Does anybody have ideas on how to fix this?

user788171
  • 16,753
  • 40
  • 98
  • 125

1 Answers1

3

For anyone finding this on google: I had the same issue Includes were "functional" and "list", didnt give me any error lower_bound wasnt defined or anything, just that it couldnt find a matching function.

Including "algorithm" did the trick.. nearly an hour of searching around for such a stupid thing

imer
  • 136
  • 1
  • 8
  • I had a similar problem and this worked for me as well. I swear my code worked a while ago, but then suddenly didn't. #include did the trick. Weird. – stochastic Oct 23 '19 at 14:49