“未解决的重载函数类型”同时尝试使用for_each与C ++中的迭代器和函数 [英] "Unresolved overloaded function type" while trying to use for_each with iterators and function in C++
问题描述
//for( unsigned int i=0; i < c.size(); i++ ) tolower( c[i] );
for_each( c.begin(), c.end(), tolower );
我试图使用 for_each
我不确定为什么会收到此错误消息:
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>)â
推荐答案
撰写:
for_each( c.begin(), c.end(), ::tolower );
或:
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.
您的代码不工作的原因:还有另一个重载函数 tolower
在命名空间 std
中,这导致解析名称时出现问题,因为编译器无法确定您引用的是哪个重载,当您简单地传递 tolower
1 。这就是为什么编译器在错误消息中说未解析的重载函数类型
,表示存在过载。
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).
为了帮助编译器解决正确的重载,你必须将 tolower
作为
So to help the compiler in resolving to the correct overload, you've to cast tolower
as
(int (*)(int))tolower
编译器获得提示以选择全局 tolower
函数,在其他方式中,可以通过写 :: tolower
。
then the compiler gets the hint to select the global tolower
function, which in other ways, can be used by writing ::tolower
.
1。我想你在代码中使用命名空间std 写了。我也建议你不要这样做。一般使用完全限定名。
顺便说一下,我想要转换输入string into小写,如果是这样,那么 std :: for_each
不会这样做。您必须使用 std :: transform
函数作为
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.
这篇关于“未解决的重载函数类型”同时尝试使用for_each与C ++中的迭代器和函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!