任何人都可以告诉我这个程序中的错误是什么
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str = "Now";
transform(str.begin(), str.end(), str.begin(), toupper);
cout<<str;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
"no matching function for call to 'transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded function type>)'
compilation terminated due to -Wfatal-errors."
Run Code Online (Sandbox Code Playgroud)
awe*_*oon 11
名称有两个函数toupper.一个来自cctype标题:
int toupper( int ch );
Run Code Online (Sandbox Code Playgroud)
locale标题中排名第二:
charT toupper( charT ch, const locale& loc );
Run Code Online (Sandbox Code Playgroud)
编译器无法推断出应该使用哪个函数,因为您允许使用命名空间std.您应该使用范围解析operator(::)来选择在全局空间中定义的函数:
transform(str.begin(), str.end(), str.begin(), ::toupper);
Run Code Online (Sandbox Code Playgroud)
或者,更好:不要使用using namespace std.
感谢@Praetorian -
这可能是错误的原因,但添加
::可能并不总是有效.如果cctypetoupper不保证在全局命名空间中存在包含.演员可以提供必要的消歧static_cast<int(*)(int)>(std::toupper)
所以,电话应该是这样的:
std::transform
(
str.begin(), str.end(),
str.begin(),
static_cast<int(*)(int)>(std::toupper)
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7734 次 |
| 最近记录: |