//for( unsigned int i=0; i < c.size(); i++ ) tolower( c[i] );
for_each( c.begin(), c.end(), tolower );
我正在尝试使用 for_each
循环代替 for 循环进行赋值。
我不确定为什么会收到此错误消息:
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>)â
最佳答案
写:
for_each( c.begin(), c.end(), ::tolower );
或者:
for_each( c.begin(), c.end(), (int(*)(int))tolower);
我遇到过这个问题很多次,以至于我厌倦了在我的代码以及其他人的代码中修复这个问题。
您的代码无法正常工作的原因:命名空间 std
中还有另一个重载函数 tolower
导致解析名称时出现问题,因为编译器无法当您简单地传递 tolower
1 时,决定您指的是哪个重载。这就是为什么编译器在错误消息中说 unresolved overloaded function type
的原因,这表明存在重载。
因此,为了帮助编译器解决正确的重载问题,您必须将 tolower
转换为
(int (*)(int))tolower
然后编译器得到选择全局tolower
函数的提示,在其他方面,可以通过编写::tolower
来使用它。
1。我猜您已经在代码中编写了 using namespace std
。我也建议你不要那样做。通常使用完全限定名称。
顺便说一下,我认为你想将输入字符串转换为小写,如果是这样,那么 std::for_each
不会那样做。您必须使用 std::transform
函数作为:
std::string out;
std::transform(c.begin(), c.end(), std::back_inserter(out), ::tolower);
//out is output here. it's lowercase string.
关于c++ - "Unresolved overloaded function type"尝试将 for_each 与 C++ 中的迭代器和函数一起使用时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7531866/