-3

I implemented the accepted answer to this question which uses std::isdigit:

#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();
}

It works fine on Mac OS X. However when using MSVC in VS17 Professional 15.9.3 for Windows 10, I get this compiler error:

C2672 'std::isdigit': no matching overloaded function found.

I am porting code and KNOW this works on Mac OS X and clang. I have included <string>, <cctype>, <ctype.h> and <stdlib.h> nothing helps.

It makes no sense to me.

  • 1
    Please provide [mre]. It states that no matching **overloaded** function found. This has nothing to do with Windows, since I can use `std::digit` just fine with vc++: https://rextester.com/YMZII3430 – Algirdas Preidžius Nov 19 '19 at 23:24
  • 1
    How about you include the actual *code* ? Further, you didn't link an *answer* ; you linked a question, which has a multitude of answers, and I'd rather not waste time guessing which answer you're talking about. Include the code, ideally in a [mcve] in *this* question. – WhozCraig Nov 19 '19 at 23:25
  • Should I include the code from the answer I linked to or is it enough to link to it? I'm new to this, let me know thx. – pifthemighty Nov 19 '19 at 23:27
  • @pifthemighty 1) "_Should I include the code from the answer I linked to or is it enough to link to it?_" You should include [mre], of the code **you** are compiling. 2) "_I'm new to this_" Did you take the [tour], and read through [ask], and [help], before asking? – Algirdas Preidžius Nov 19 '19 at 23:27
  • Your question should be self-contained. Any links are for *assistance*, not *dependence*. A *full* [mcve] along with verbatim error messages and line numbers comment-marked (but *not* included in source) is ideal. – WhozCraig Nov 19 '19 at 23:28
  • You say you have included ``, ``, and ``. You also need to include ``. With some implementations `` is included by other headers but that is NOT required. – Peter Nov 19 '19 at 23:44
  • Try including both `` and ``. – AProgrammer Nov 20 '19 at 15:32
  • Sorry about that. I added the includes, my bad for not having a complete example. – pifthemighty Nov 20 '19 at 15:42

2 Answers2

0

The function isdigit() is define in the C header ctype.h or the C++ header cctype.

If you include ctype.h from your C++ file then isdigit() is defined in the global namespace (and some implementations also place in in std::).

Conversely if you include cctype from your C++ file then isdigit() is defined in the standard namespace std:: (and some implementations also place in the global namespace).

In your case you have #included <ctype.h> and on windows it looks like that means it is only available in the global namespace. Thus std::isdigit() does not match any known functions. Try changing your include to #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();
}
Martin York
  • 234,851
  • 74
  • 306
  • 532
  • I like this answer. I tried this and I've had no luck. I've switched between `#include ` and `#include ` and then both and neither, none of it seems to help. The only thing that works is changing the line `while (it != s.end() && std::isdigit(*it)) ++it;` to `while (it != s.end() && isdigit(*it)) ++it;`. I'm not entirely sure why but I think this answer seems to shed some light on what might be going on behind the scenes. – pifthemighty Nov 20 '19 at 15:30
  • @pifthemighty Then you have a non conformant tools. What compiler (and version of compiler) are you using. Might also be useful to known what version of Windows you are using. – Martin York Nov 20 '19 at 15:44
-2

As it turns out, it is a similar problem to this. There is some ambiguity when you use the std namespace, i.e., using namespace std;. If you change std::isdigit in the code to isdigit it will compile on Windows.

#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() && isdigit(*it)) ++it;
    return !s.empty() && it == s.end();
}
  • 2
    "_There is some ambiguity when you use the std namespace_" There are good reasons, why one [shouldn't be `using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) at all. – Algirdas Preidžius Nov 19 '19 at 23:29
  • 1
    The linked question only occurs when taking the address of the function, but your posted code does not do that – M.M Nov 20 '19 at 01:25