0

I'm using g++ 4.7.

What I'm trying to do is this,

find_if(s.begin(), s.end(), isalnum);

where isalnum is defined in cctype and s is a string.

logman.cpp:68:47: error: no matching function for call to ‘find_if(std::basic_string<char>::const_iterator, std::basic_string<char>::const_iterator, <unresolved overloaded function type>)’

However, this works,

bool my_isalnum(int c) {
    return isalnum(c);
}

find_if(s.begin(), s.end(), my_isalnum);

How can I get this to work without creating my own function?

Gustavo Muenz
  • 9,278
  • 7
  • 40
  • 42
gsgx
  • 12,020
  • 25
  • 98
  • 149

3 Answers3

8

The compiler is having trouble disambiguating between this function and this function. You want the first one, and you'll have to help the compiler out here, by specifying the signature with a cast:

find_if(s.begin(), s.end(), (int(*)(int))isalnum);
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
2

This should work.

#include <algorithm>
#include <cctype>
auto result = std::find_if (begin(s), end(s), std::isalnum);
Calthron
  • 21
  • 1
0

This should work

#include <algorithm >
#include <cctype>

auto result = std::find_if(std::begin(s), std::end(s),  isalnum) ;
vimuth
  • 5,064
  • 33
  • 79
  • 116
Owl66
  • 147
  • 12