std :: numeric_limits :: max的语法错误 [英] Syntax error with std::numeric_limits::max

查看:117
本文介绍了std :: numeric_limits :: max的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类结构定义如下:

#include <limits>

struct heapStatsFilters
{
    heapStatsFilters(size_t minValue_ = 0, size_t maxValue_ = std::numeric_limits<size_t>::max())
    { 
         minMax[0] = minValue_; minMax[1] = maxValue_; 
    }

    size_t minMax[2];
};

问题是我不能使用'std :: numeric_limits :: max()'和编译器说:

The problem is that I cannot use 'std::numeric_limits::max()' and the compiler says:

错误8错误C2059:语法错误:'::'

错误7错误C2589:'(':'::'

我正在使用的编译器是Visual C ++ 11(2012)

The compiler which I am using is Visual C++ 11 (2012)

推荐答案

您的问题是由<$引起的c $ c>< Windows.h> 头文件,其中包含名为 max min

Your problem is caused by the <Windows.h> header file that includes macro definitions named max and min:

#define max(a,b) (((a) > (b)) ? (a) : (b))

看到此定义,预处理器将替换 max 表达式中的标识符:

Seeing this definition, the preprocessor replaces the max identifier in the expression:

std::numeric_limits<size_t>::max()

通过宏定义,最终导致无效的语法:

by the macro definition, eventually leading to invalid syntax:

std::numeric_limits<size_t>::(((a) > (b)) ? (a) : (b))

在编译器中报告错误:' (':'::'右侧的非法令牌。

reported in the compiler error: '(' : illegal token on right side of '::'.

作为解决方法,您可以添加 NOMINMAX 定义为编译器标志(或转换单元,包括标题):

As a workaround, you can add the NOMINMAX define to compiler flags (or to the translation unit, before including the header):

#define NOMINMAX   

或将调用包装为 max 加上括号,可以防止宏扩展:

or wrap the call to max with parenthesis, which prevents the macro expansion:

size_t maxValue_ = (std::numeric_limits<size_t>::max)()
//                 ^                                ^

#undef max ,然后调用 numeric_limits< size_t> :: max()

#undef max
...
size_t maxValue_ = std::numeric_limits<size_t>::max()

这篇关于std :: numeric_limits :: max的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆