0

I have code like this:

// .h file
#include <iostream>
#include <limits>
 
class MyClass
{
public:
    myInput();
    int inputValue; 
}
 
 
// .cpp file
#include "MyClass.h"
void MyClass::myInput()
{
    std::cin >> inputValue;
    if (!std::cin)
    {
        //fix error
        std::cin.clear();
        std::cin.ignore(std::numeric_limits < std::streamsize >::max(), '\n');
    }
}

During compilation, the compiler is showing error:

Expected an identifier

In the line with

std::cin.ignore(std::numeric_limits < std::streamsize >::max(), '\n');

Moreover, the ::max() is underlined. I don't have an idea what is going on. Thanks for help!

Mavimix
  • 119
  • 1
  • 8
  • 1
    What happens if you use `std::numeric_limits::max()` instead? – NathanOliver Jun 07 '21 at 16:04
  • This is one of the few cases where spaces matter. – Mooing Duck Jun 07 '21 at 16:06
  • 2
    [works for me](https://godbolt.org/z/nE1z9bhf4), please show a [mre]. My guess would be a `max` macro creeping in from somewhere (e.g. from `windows.h`) – Alan Birtles Jun 07 '21 at 16:07
  • Problem has solved. I'm using Visual Studio which have macro called `max`. So I added `#undef max;` at the begin of my file and everything is working. Thanks for help. – Mavimix Jun 07 '21 at 16:22
  • This doesn't address the question, but in general, source files of all kinds should only `#include` the headers that they need. The definition of `MyClass` doesn't use anything from the standard library, so the .h file that doesn't need any `#include` directives. Tthe .cpp file uses iostreams and numeric_limits, so it should have `#include ` and `#include `. The code works fine as written; it's just that it's more efficient to only `#include` things that you need, rather than forcing those `#include` directives into any source file that uses that header. – Pete Becker Jun 07 '21 at 16:42

1 Answers1

2

Problem has solved. I'm using Visual Studio which have macro called max. So I added #undef max; at the begin of my file and everything is working. Thanks for help.

Mavimix
  • 119
  • 1
  • 8
  • this not the correct way. You must [`#define NOMINMAX` before including windows.h](https://stackoverflow.com/q/13416418/995714). Besides it's highly recommended to [#define `WIN32_LEAN_AND_MEAN`](https://stackoverflow.com/q/11040133/995714) and possibly `VC_EXTRALEAN` before including that also – phuclv Jun 07 '21 at 17:16