C++ “numeric_limits”未在此范围内声明,没有用于调用“max()”的匹配函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4798936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:32:36  来源:igfitidea点击:

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

c++maxignorecinnumeric-limits

提问by Matt Munson

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:

我在家里用我的 mac w/xcode 编译了这段代码,没有任何问题。我在学校用 g++ 在 linux 上编译它,我得到这些错误:

:‘numeric_limits' is not a member of std
:expected primary-expression before ‘>' token
:no matching function for call to ‘max()'

:'numeric_limits' 不是 std 的成员
:在 '>' 标记之前预期的主表达式
:没有用于调用 '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 geussing maybe I have to include an extra header. If I take away the std:: it just gives me a similar error

我猜也许我必须包含一个额外的标题。如果我拿走 std:: 它只会给我一个类似的错误

‘numeric_limits' was not declared in this scope

'numeric_limits' 未在此范围内声明

回答by Adam Rosenfield

You need to include the header file <limits>, which is where std::numeric_limitsis 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.

您需要包含头文件<limits>,这std::numeric_limits是定义的位置。您的 Mac 编译器通过自动包含该头文件来帮助您;但是,您不应该依赖该行为并明确包含您需要的任何头文件。