c++ - 为什么 “std::isdigit”在Windows上的C++中报告 “no match

我使用std::isdigit实现了这个question的可接受答案:

#include <string>
#include <ctype.h>

using namespace std;

bool is_number(const std::string& s)
{
    std::string::const_iterator it = s.begin();
    while (it != s.end() && std::isdigit(*it)) ++it;
    return !s.empty() && it == s.end();
}

它在Mac OS X上运行良好。但是,在Windows 10的VS17 Professional 15.9.3中使用MSVC时,出现此编译器错误:
C2672 'std::isdigit': no matching overloaded function found.

我正在移植代码,并且知道它可以在Mac OS X和clang上使用。我已经包含<string><cctype><ctype.h><stdlib.h>没有任何帮助。

对我来说完全是无稽之谈。

最佳答案

函数isdigit()在C头ctype.h或C++头cctype中定义。

如果您从C++文件中包含ctype.h,则isdigit()是在全局 namespace 中定义的(某些实现也位于std::中)。

相反,如果您从C++文件中包含cctype,则isdigit()是在标准 namespace std::中定义的(某些实现也位于全局 namespace 中)。

在您的情况下,您具有#included <ctype.h>,并且在Windows上看起来像这意味着它仅在全局 namespace 中可用。因此std::isdigit()与任何已知函数都不匹配。尝试将include更改为#include <cctype>

#include <cctype>
bool is_number(const std::string& s)
{
    std::string::const_iterator it = s.begin();
    while (it != s.end() && std::isdigit(*it)) ++it;
    return !s.empty() && it == s.end();
}

关于c++ - 为什么 “std::isdigit”在Windows上的C++中报告 “no matching overloaded function”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58944350/

相关文章:

python - 使用 MinGW+GCC 编译 SciPy 时编译器失败

python - mysql-python的pip安装错误无法正常工作

c++ - 在此范围C++中未声明错误 'menu'

.net - 使用xmlrpc和vb.net在wordpress中发布错误

c++ - 我的程序未发现struct中的功能

c# - 如何正确地将 Dictionary,Object

delphi - delphi编译错误 "[DCC Error] ProjectName.dpr([

c - 使用指针的程序运行不正常

java - “Cannot find symbol class keyboard reader”

c++ - LNK2019未解析的外部符号。创建二叉树