C++ 错误:“INT32_MAX”未在此范围内声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3233054/
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 12:23:50  来源:igfitidea点击:

error: 'INT32_MAX' was not declared in this scope

c++

提问by jm1234567890

I'm getting the error

我收到错误

error: 'INT32_MAX' was not declared in this scope

But I have already included

但我已经包括

#include <stdint.h>

I am compiling this on (g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44) with the command

我正在使用命令在 (g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44) 上编译它

g++ -m64 -O3 blah.cpp

Do I need to do anything else to get this to compile? or is there another C++ way to get the constant "INT32_MAX"?

我需要做任何其他事情来编译它吗?还是有另一种 C++ 方法来获取常量“ INT32_MAX”?

Thanks and let me know if anything is unclear!

谢谢,如果有什么不清楚的,请告诉我!

采纳答案by doc

 #include <cstdint> //or <stdint.h>
 #include <limits>

 std::numeric_limits<std::int32_t>::max();

Note that <cstdint>is a C++11 header and <stdint.h>is a C header, included for compatibility with C standard library.

请注意,这<cstdint>是一个 C++11 头文件,并且<stdint.h>是一个 C 头文件,为了与 C 标准库兼容而包含在内。

Following code works, since C++11.

以下代码有效,自 C++11 起。

#include <iostream>
#include <limits>
#include <cstdint>

struct X 
{ 
    static const std::int32_t i = std::numeric_limits<std::int32_t>::max(); 
};

int main()
{
    switch(std::numeric_limits<std::int32_t>::max()) { 
       case std::numeric_limits<std::int32_t>::max():
           std::cout << "this code works thanks to constexpr\n";
           break;
    }
    return EXIT_SUCCESS;
}

http://coliru.stacked-crooked.com/a/4a33984ede3f2f7e

http://coliru.stacked-crooked.com/a/4a33984ede3f2f7e

回答by Blindy

Quoted from the man page, "C++ implementations should define these macros only when __STDC_LIMIT_MACROSis defined before <stdint.h>is included".

引用自手册页,“C++ 实现应仅在包含__STDC_LIMIT_MACROS之前定义时定义这些宏<stdint.h>”。

So try:

所以尝试:

#define __STDC_LIMIT_MACROS
#include <stdint.h>

回答by pixelpax

Hm... All I needed to do was #include <climits>nothing else on this page worked for me.

嗯...我需要做的就是#include <climits>这个页面上没有其他东西对我有用。

Granted, I was trying to use INT_MIN.

当然,我正在尝试使用INT_MIN.

回答by Rahul Dutta

#include <iostream>
#include <limits.h> or <climits>

worked for me

为我工作

回答by Ram

I ran into similar issue while using LLVM 3.7.1 c++ compiler. Got the following error while trying to compile gRPC code as part of building some custom library

我在使用 LLVM 3.7.1 c++ 编译器时遇到了类似的问题。尝试编译 gRPC 代码作为构建某些自定义库的一部分时出现以下错误

src/core/lib/iomgr/exec_ctx.h:178:12: error: use of undeclared identifier 'INT64_MAX'

The compilation went through after adding -D__STDC_LIMIT_MACROSas one of the options to c++ compiler.

在添加-D__STDC_LIMIT_MACROS为 c++ 编译器的选项之一后,编译通过。

.../bin/c++ -D__STDC_LIMIT_MACROS -I{LIBRARY_PATHS} testlib.cc -o testlib

回答by hardik chugh

I was also facing the similar problem,just need to add- #include <limits.h>after #include <iostream>

我也遇到了类似的问题,只需要在#include <limits.h>后面加上- #include <iostream>

回答by hardik chugh

Including the following code after #include <iostream>worked for me:

#include <iostream>在为我工作后包括以下代码:

#include <limits.h>

I am using the following compiler:

我正在使用以下编译器:

g++ 5.4.0-6

克++ 5.4.0-6