“未解析的重载函数类型”尝试在 C++ 中将 for_each 与迭代器和函数一起使用时;
//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( 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>)â
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
写:
或者:
我已经多次遇到这个问题,以至于我厌倦了在我的代码以及其他人的代码中修复这个问题。
您的代码无法正常工作的原因:命名空间
std
中有另一个重载函数tolower
,它在解析名称时导致问题,因为编译器无法决定哪个重载你指的是,当你简单地传递tolower
1 时。这就是为什么编译器在错误消息中显示未解析的重载函数类型
,这表明存在重载。因此,为了帮助编译器解决正确的重载问题,您必须强制转换
tolower
,然后编译器会收到选择全局
tolower
函数的提示,这在其他方面,可以通过编写::tolower
来使用。1.我猜您已经在代码中编写了
using namespace std
。我还建议您不要这样做。一般情况下使用完全限定名称。顺便说一句,我认为您希望将输入字符串转换为小写,如果是这样,那么
std::for_each
不会这样做。您必须使用std::transform
函数:Write:
Or :
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 namespacestd
which is causing problem when resolving the name, because the compiler is unable to decide which overload you're referring to, when you simply passtolower
1. That is why the compiler is sayingunresolved 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
asthen 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 usestd::transform
function as:1)您的代码中某处有
using namespace std;
。导入整个 std 命名空间的危险在于您不一定知道您将得到什么。在本例中,您导入了std::tolower
的重载。切勿输入
using namespace std;
,即使您的教科书或讲师告诉您这样做。2) 由于您被限制使用
std::transform
,您可以使用std::for_each
修改字符串: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 ofstd::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 usingstd::for_each
: