9
//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>)â

2 Answers 2

19

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.
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much! Unfortunately this is a STL class & were not supposed to use transform yet.
If he wants to modify the input string in-place, he can provide his own function to std::for_each. Like this.
@bluetickk: But you're using std::for_each anyway. Why can't you use std::transform?
Note too that this version of tolower has undefined behavior on most systems when passed a char.
0

1) You have using namespace std; somewhere in your code. The danger of importing the entire std namespace is that you don't necessarily know what you are getting. In this case, you have imported overloads of std::tolower.

Never type using namespace std;, even if your textbook or your instructor tells you to.

2) Since you are restricted from using std::transform, you could modify the string in place using std::for_each:

#include <cctype>
#include <algorithm>
#include <string>
#include <iostream>

void
MakeLower(char& c)
{
  c = std::tolower(c);
}

int
main ()
{
  std::string c("Hello, world\n");
  std::for_each(c.begin(), c.end(), MakeLower);
  std::cout << c;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.