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

问题描述 投票:30回答:7

我遇到错误

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”?

谢谢,让我知道是否有任何不清楚的地方!

c++
7个回答
23
投票
 #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 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


47
投票

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

所以尝试:

#define __STDC_LIMIT_MACROS
#include <stdint.h>

7
投票

嗯...我要做的就是#include <climits>此页面上没有其他对我有用。

授予,我正在尝试使用INT_MIN


3
投票
#include <iostream>
#include <limits.h> or <climits>

为我工作]


0
投票

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


0
投票

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.