tags:

views:

64

answers:

2

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!

+3  A: 

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

So try:

#define __STDC_LIMIT_MACROS
#include <stdint.h>
Blindy
Thanks! I missed that when reading the man page -_-, I need to wait 7 min before accepting your answer though...
jm1234567890
+3  A: 
 #include <stdint.h> //or <cstdint>
 #include <limits>

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

but please note that <cstdint> nor <stdint.h> (which is C header) isn't a part of C++ standard library.

doc
Note that this isn't equivalent as it can't be used as a compile-time constant (pre-C++0x that is).
Georg Fritzsche
yes! This is what I am looking for, a C++ way to do it. Thanks
jm1234567890
@Georg Fritzsche: it can be used as compile-time constant. `const int MY_INT32_MAX = std::numeric_limits<int32_t>::max();` will work just fine. This method is so simple that it will be optimized out as it would be explicitly defined constant.
doc
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]).
tjm
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 Fritzsche