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

新手上路,请多包涵

我收到错误

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

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

原文由 jm1234567890 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
2 个回答
 #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

原文由 mip 发布,翻译遵循 CC BY-SA 4.0 许可协议

包括 –> #include <bits/stdc++.h> <–

应该看起来像:

 #include<iostream>
using namespace std;
#include <bits/stdc++.h>

原文由 Devansh Raut 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题