//for( unsigned int i=0; i < c.size(); i++ ) tolower( c[i] );
for_each( c.begin(), c.end(), tolower );
I am trying to use a for_each
loop in place of the for loop for an assignment.
I am unsure why I am getting this error message:
In function âvoid clean_entry(const std::string&, std::string&)â:
prog4.cc:62:40: error: no matching function for call to âfor_each(std::basic_string<char>::iterator, std::basic_string<char>::iterator, <unresolved overloaded function type>)â
Write:
for_each( c.begin(), c.end(), ::tolower );
Or :
for_each( c.begin(), c.end(), (int(*)(int))tolower);
I've faced this problem so many times that I'm tired of fixing this in my code, as well as in others' code.
Reason why your code is not working : there is another overloaded function tolower
in the namespace std
which is causing problem when resolving the name, because the compiler is unable to decide which overload you're referring to, when you simply pass tolower
1. That is why the compiler is saying unresolved overloaded function type
in the error message, which indicates the presence of overload(s).
So to help the compiler in resolving to the correct overload, you've to cast tolower
as
(int (*)(int))tolower
then the compiler gets the hint to select the global tolower
function, which in other ways, can be used by writing ::tolower
.
1. I guess you've written using namespace std
in your code. I would also suggest you to not to do that. Use fully-qualified names in general.
By the way, I think you want to transform the input string into lower case, if so, then std::for_each
wouldn't do that. You've to use std::transform
function as:
std::string out;
std::transform(c.begin(), c.end(), std::back_inserter(out), ::tolower);
//out is output here. it's lowercase string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With