1

Here's my code snippet:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;


bool next(int j)
{
    return(j<3);
}

int main()
{
    ......
    cin>>m;
    int h[m];
    memset(h, 0, sizeof(h));
    .......

    int *p;
    p = find_if(h, h+m, next);
    ......
}

I'm getting the following error upon compilation:

no matching function for call to ‘find_if(int*, int*, )’

template _IIter std::find_if(_IIter, _IIter, _Predicate)

template argument deduction/substitution failed:

couldn't deduce template parameter ‘_Predicate’

Community
  • 1
  • 1
geedee
  • 45
  • 7

1 Answers1

5

You are a victim of C++'s somewhat arcane lookup rules!

Because you didn't qualify next, and because you wrote using namespace std, and because std::next exists, the compiler considers std::next a candidate (even though the lookup then fails) and nothing else!

Qualify your function:

find_if(h, h+m, ::next);
//              ^^

A testcase representing your code:

#include <algorithm>
using namespace std;

bool next(int j)
{
    return (j<3);
}

int main()
{
    int h[5] = {};
    const size_t m = 2;
    find_if(h, h+m, next);
}

(live demo)

And fixed:

#include <algorithm>
using namespace std;

bool next(int j)
{
    return (j<3);
}

int main()
{
    int h[5] = {};
    const size_t m = 2;
    find_if(h, h+m, ::next);
}

(live demo)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    @geedee see, this is a [MCVE]. That is expected of you to post in order to make our life easier when trying to help you. Please from now on do take the time to create a proper MCVE for your questions. – bolov Mar 05 '18 at 11:16