//for( unsigned int i=0; i < c.size(); i++ ) tolower( c[i] );
for_each( c.begin(), c.end(), tolower );
Run Code Online (Sandbox Code Playgroud)
我试图使用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>)â
Run Code Online (Sandbox Code Playgroud)
Naw*_*waz 17
写:
for_each( c.begin(), c.end(), ::tolower );
Run Code Online (Sandbox Code Playgroud)
要么 :
for_each( c.begin(), c.end(), (int(*)(int))tolower);
Run Code Online (Sandbox Code Playgroud)
我已经多次面对这个问题了,我已经厌倦了在我的代码和其他代码中解决这个问题.
您的代码无法正常工作的原因:tolower命名空间中有另一个重载函数,std在解析名称时会导致问题,因为当您只是传递tolower 1时,编译器无法确定您所指的是哪个重载.这就是编译器unresolved overloaded function type在错误消息中说的原因,它表明存在重载.
因此,为了帮助编译器解决正确的重载,你需要转换tolower为
(int (*)(int))tolower
Run Code Online (Sandbox Code Playgroud)
然后编译器获取提示以选择全局tolower函数,在其他方面,可以通过写入来使用::tolower.
我猜你已经写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.
Run Code Online (Sandbox Code Playgroud)