C++ cctype怎么了?

C++ cctype怎么了?,c++,namespaces,std,C++,Namespaces,Std,令我惊讶的是,编译了以下代码: #include <iostream> #include <string> #include <algorithm> #include <iterator> #include <cctype> int main() { std::string s="nawaz"; std::string S; std::transform(s.begin(),s.end(), std::back_in

令我惊讶的是,编译了以下代码:

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <cctype>

int main() {
   std::string s="nawaz";
   std::string S;
   std::transform(s.begin(),s.end(), std::back_inserter(S), ::toupper);
   std::cout << S ;
}
第一个问题解决了,但如果我也更改上面的
transform()
行:

std::transform(s.begin(),s.end(), std::back_inserter(S), std::toupper);
我现在希望它现在也能编译。但我得到一个编译器错误:

kk.cpp:12:错误:调用“transform(\uuu gnu\u cxx::\uu normal\u迭代器,\uu gnu\u cxx::\uu normal\u迭代器,std::back\u insert\u迭代器,)时没有匹配的函数

手动编辑也解决了这一问题:

kk.cpp:12: error: no matching function for call to
         `transform(iterator<std::string>,
                    iterator<std::string>,
                    std::back_insert_iterator<std::string>,
                    <unresolved overloaded function type>)'
kk.cpp:12:错误:没有用于调用的匹配函数
`变换(迭代器,
迭代器,
std::back\u insert\u迭代器,
)'
<>我错过了什么?

你错过了C++也在代码< > /Cord>中添加了新的<代码> ToupPu/Cuff>函数,这可能被你的其他头包含在内。因此,在
std::
名称空间中有多个重载,而在全局名称空间中只有旧的C版本的函数


也就是说,g++似乎仍然能够推断出正确的重载。

它不起作用,因为存在
std::toupper
的重载。您可以通过强制转换到所需的函数重载来修复它:

std::transform(s.begin(),s.end(), std::back_inserter(S),
                (int(&)(int))std::toupper);

+谢谢你问这个问题。连我都想知道!在我看来像是一个GCC错误。我在VC中编译它没有问题。啊,你只是忍不住对g++的输出报以微笑。它们充满了爱。我做了一些研究,认为std::toupper和::toupper在某些方面是不同的,转换无法处理,但我找不到任何东西。Comeau Online也编译代码,Intel C++也编译代码。这是一个更好的答案。也发现了一个类似的讨论。如果我看到一个C++版本(cand and char char)的话,就有一个新的版本来介绍CcType函数。这些函数接受并返回int,以便处理EOF字符。还显示我应该学会读取错误消息。它解释了确切的问题。@GMan:我认为问题不在于应该采取什么措施使它起作用;毕竟,
::toupper
已经可以工作了。所以问题是,
std::toupper
为什么不起作用?它是否应该像我们明确提到的命名空间
std
那样工作?@Nawaz:是的,从导入
::toupper
和从
模板C toupper(C C,const locale&l)。仅指定
std::toupper
对于参数无效,因为它可以是。通过铸造,它消除了一个过载。
std::transform(s.begin(),s.end(), std::back_inserter(S),
                (int(&)(int))std::toupper);