c++maxcinnumeric-limits

‘numeric_limits’ was not declared in this scope, no matching function for call to ‘max()’


I compiled this code at home on my mac w/ xcode and there was no provblem. I compile it at school with g++ on linux and I get these errors:

numeric_limits’ is not a member of std

expected primary-expression before ‘>’ token

no matching function for call to ‘max()’

#include <iostream>
#include <cstdlib>

using namespace std;

int GetIntegerInput(int lower, int upper)
{
    int integer = -1;
    do
    {
        cin >> integer;
        cin.clear();
        cin.ignore(std::numeric_limits<streamsize>::max(), '\n');  //errors here
    } while (integer < lower || integer > upper);

    return integer;
}

I'm guessing maybe I have to include an extra header. If I take away the std:: it just gives me a similar error:

numeric_limits was not declared in this scope


Solution

  • You need to include the header file <limits>, which is where std::numeric_limits is defined. Your Mac compiler was helping you out by automatically including that header file; however, you should not rely on that behavior and explicitly include any header files you need.