使用numeric_limits max min的编译问题

numeric_limit提供数据类型信息,比如最大/最小值,有符号,是否有限等等,STL库已经具现化(实例化)char, bool, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, float, double和long double. 自定义类也可以通过具现化该类提供相关信息。

  1. #include <limits> 
  2. long maxLong = std::numeric_limits<COLORREF>::max();

会出现编译错误

error C2589: '(' : illegal token on right side of '::'
error C2143: syntax error : missing ';' before '::'

错误的原因是编译器认为是这个max是winDef.h中的max宏。

  1. #ifndef max
  2. #define max(a,b)            (((a) > (b)) ? (a) : (b))
  3. #endif

解决办法

1、修改语句

  1. #include <limits>  
  2. long maxLong = (std::numeric_limits<long>::max)());

2、 undef windows max宏

  1. #ifdef max
  2.     #undef max
  3.             std::numeric_limits<COLORREF>::max();
  4. #endif

3、如果觉得1,2麻烦,也可以直接使用INT_MAX,LLONG_MAX。

猜你喜欢

转载自blog.csdn.net/wyhjia/article/details/6605258