''INT32_MAX'' was not declared in this scope

c++ - error: 'INT32_MAX' was not declared in this scope - Stack Overflow

Ask Question

Asked 10 years, 11 months ago

Active 3 months ago

Viewed 64k times

38

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++ -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"?

Thanks and let me know if anything is unclear!

c++

Share

asked Jul 12 '10 at 23:27

jm1234567890

1,45222 gold badges1818 silver badges2929 bronze badges

  • 2

    @WalterTross baaaad baad idea, – IanNorton Jan 8 '14 at 10:17

  • @IanNorton, maybe you are right, but I still have to see a place where INT32_MAX is a different value – Walter Tross Jan 8 '14 at 14:35

  • My Eclipse (Helios)/MinGW setup did not highlight <limits> as an error, and I was able to right click and open declaration, but I had to change 'include<limits>' to 'include <limits.h>' in order to stop INT_MAX reporting a 'not declared in this scope' error. – Clarius Feb 25 '17 at 9:55

Add a comment

//======================================================

7 Answers

24
#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.

Following code works, since 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

answered Jul 12 '10 at 23:41
doc

7,52055 gold badges4242 silver badges6767 bronze badges
  • 5
    Note that this isn't equivalent as it can't be used as a compile-time constant (pre-C++0x that is). – Georg FritzscheJul 12 '10 at 23:43
  • 1
    yes! This is what I am looking for, a C++ way to do it. Thanks – jm1234567890Jul 12 '10 at 23:44
  • 1
    If you use <cstdint> and compile with -std=c++0x, INT32_MAX is defined as a constant anyway (At least over here it is [GCC 4.4.4]). – tjmJul 12 '10 at 23:53
  • 5
    Doc, its not a compile-time constant - you can't use that where the language requires an integral constant. Try switch(1) { case std::numeric_limits<int32_t>::max(): } or struct X { static const int i = std::numeric_limits<int32_t>::max(); };. I'm btw not saying <limits> is bad, just pointing out a difference. – Georg FritzscheJul 13 '10 at 1:50
  • 2
    Pedantic: need to qualify std::int32_t, since the .h wasn't included and for the sake of good practice in general. – PotatoswatterJul 22 '15 at 14:55
Show 2 more comments
49

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

So try:

#define __STDC_LIMIT_MACROS#include <stdint.h>
Lightness Races in Orbit

360k6868 gold badges597597 silver badges991991 bronze badges
answered Jul 12 '10 at 23:30
Blindy

55.5k99 gold badges8181 silver badges120120 bronze badges
  • Thanks! I missed that when reading the man page -_-, I need to wait 7 min before accepting your answer though... – jm1234567890Jul 12 '10 at 23:35
  • 1
    This should be the accepted answer. Also make sure you put that before any other include files that might include stdint.h without setting that define first. – Keith JohnstonApr 23 '15 at 18:36
  • 2
    5 years later... still not the accepted answer :p Thanks, Blindy. That is a big help. – Spencer DMay 15 '15 at 18:44
  • To be fair the accepted answer is just as good, arguably a more C++ way of doing things. This one is good to know too however. – BlindyMay 15 '15 at 21:06
  • 2
    This was a gcc bug: sourceware.org/bugzilla/show_bug.cgi?id=15366– TechnophileDec 1 '17 at 20:51
Show 5 more comments
8

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

Granted, I was trying to use INT_MIN.

answered Sep 21 '17 at 2:04
pixelpax

1,19099 silver badges2121 bronze badges
Add a comment
6
#include <iostream>#include <limits.h> or <climits>

worked for me

answered Feb 5 '19 at 18:14
Rahul Dutta

6111 silver badge22 bronze badges
Add a comment
0

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

#include <limits.h>

I am using the following compiler:

g++ 5.4.0-6

answered Jun 20 '17 at 22:50
user8024721
Add a comment
0

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

answered Dec 21 '18 at 10:27
hardik chugh

60888 silver badges1010 bronze badges
Add a comment
0

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

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_MACROS as one of the options to c++ compiler.

.../bin/c++ -D__STDC_LIMIT_MACROS -I{LIBRARY_PATHS} testlib.cc -o testlib
answered Aug 5 '19 at 21:29
Ram

111 bronze badge
Add a comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged c++ or ask your own question.

(0)

相关推荐