“未解析的重载函数类型”尝试在 C++ 中将 for_each 与迭代器和函数一起使用时;

发布于 2024-12-06 05:22:01 字数 506 浏览 1 评论 0原文

//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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对风讲故事 2024-12-13 05:22:01

写:

for_each( c.begin(), c.end(), ::tolower );

或者:

for_each( c.begin(), c.end(), (int(*)(int))tolower);

我已经多次遇到这个问题,以至于我厌倦了在我的代码以及其他人的代码中修复这个问题。

您的代码无法正常工作的原因:命名空间 std 中有另一个重载函数 tolower ,它在解析名称时导致问题,因为编译器无法决定哪个重载你指的是,当你简单地传递 tolower 1 时。这就是为什么编译器在错误消息中显示未解析的重载函数类型,这表明存在重载。

因此,为了帮助编译器解决正确的重载问题,您必须强制转换 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.

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.
鹿港巷口少年归 2024-12-13 05:22:01

1)您的代码中某处有 using namespace std; 。导入整个 std 命名空间的危险在于您不一定知道您将得到什么。在本例中,您导入了 std::tolower 的重载。

切勿输入 using namespace std;,即使您的教科书或讲师告诉您这样做。

2) 由于您被限制使用 std::transform,您可以使用 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文