错误:未在此范围内声明'INT32_MAX'

jm1*_*890 30 c++

我收到了错误

error: 'INT32_MAX' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

但我已经包括在内了

#include <stdint.h>
Run Code Online (Sandbox Code Playgroud)

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

g++ -m64 -O3 blah.cpp
Run Code Online (Sandbox Code Playgroud)

我需要做任何其他事情才能编译吗?还是有另一种C++方式来获取常量" INT32_MAX"?

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

Bli*_*ndy 47

从手册页引用,"C++实现应该仅在包含__STDC_LIMIT_MACROS之前定义的这些宏时定义<stdint.h>".

所以尝试:

#define __STDC_LIMIT_MACROS
#include <stdint.h>
Run Code Online (Sandbox Code Playgroud)

  • 5年后......仍然没有接受答案:p谢谢,Blindy.这是一个很大的帮助. (2认同)
  • 这是一个gcc错误:https://sourceware.org/bugzilla/show_bug.cgi?id = 15366 (2认同)

doc*_*doc 23

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

 std::numeric_limits<std::int32_t>::max();
Run Code Online (Sandbox Code Playgroud)

请注意,它<cstdint>是一个C++ 11标头,<stdint.h>是一个C标头,包含与C标准库的兼容性.

以下代码工作,自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;
}
Run Code Online (Sandbox Code Playgroud)

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

  • 请注意,这不等同,因为它不能用作编译时常量(前C++ 0x). (5认同)
  • Doc,它不是*编译时*常量 - 你不能使用语言需要*积分常数*的地方.尝试`switch(1){case std :: numeric_limits <int32_t> :: max():}`或`struct X {static const int i = std :: numeric_limits <int32_t> :: max(); };`.我不是说"<limits>"很糟糕,只是指出了一个区别. (5认同)
  • 迂腐:需要限定`std :: int32_t`,因为`.h`不包括在内并且为了一般的良好实践. (2认同)

pix*_*pax 7

嗯...我需要做的#include <climits>只是此页面上没有对我有用的东西。

当然,我正在尝试使用INT_MIN


Rah*_*tta 7

#include <iostream>
#include <limits.h> or <climits>
Run Code Online (Sandbox Code Playgroud)

为我工作