38

我收到错误

error: 'INT32_MAX' was not declared in this scope

但是我已经包含了

#include <stdint.h>

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

g++ -m64 -O3 blah.cpp

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

谢谢,如果有任何不清楚的地方,请告诉我!

4

9 回答 9

50

从手册页中引用,“只有在包含__STDC_LIMIT_MACROS之前定义的情况下,C++ 实现才应该定义这些宏<stdint.h>”。

所以试试:

#define __STDC_LIMIT_MACROS
#include <stdint.h>
于 2010-07-12T23:30:51.067 回答
27
 #include <cstdint> //or <stdint.h>
 #include <limits>

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

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

从 C++11 开始,以下代码有效。

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

struct X 
{ 
    static constexpr 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

于 2010-07-12T23:41:35.993 回答
8

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

当然,我试图使用INT_MIN.

于 2017-09-21T02:04:16.213 回答
7
#include <iostream>
#include <limits.h> or <climits>

为我工作

于 2019-02-05T18:14:19.353 回答
0

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

#include <limits.h>

我正在使用以下编译器:

g++ 5.4.0-6

于 2017-06-20T22:50:32.303 回答
0

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

于 2018-12-21T10:27:31.803 回答
0

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

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

在将-D__STDC_LIMIT_MACROS作为选项之一添加到 c++ 编译器后编译通过。

.../bin/c++ -D__STDC_LIMIT_MACROS -I{LIBRARY_PATHS} testlib.cc -o testlib
于 2019-08-05T21:29:02.883 回答
0

所有 c++ 版本的代码,与 CentOS 6.0(gcc 版本 4.4.7)等较低的 GCC 兼容:

// https://onlinegdb.com/ApNzDNYUx

#include <stdio.h>

// @see https://stackoverflow.com/a/9162072/17679565
#include <inttypes.h>

// For CentOS 6(gcc version 4.4.7) or not defined the macro.
#ifndef INT32_MAX
#define INT32_MAX (2147483647)
#endif

int main()
{
    printf("INT32_MAX=%d\n", INT32_MAX);

    return 0;
}

在线GDB

于 2021-12-27T03:01:26.003 回答
-2
#include<bits/stdc++.h>

在开头添加这个。

于 2022-01-16T06:01:12.137 回答