“Unresolved overloaded function type” while trying

2020-02-01 04:16发布

问题:

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

回答1:

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.


回答2:

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;
}


标签: c++ iterator g++