C++ 调用 std::max 时出现问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2789481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:40:53  来源:igfitidea点击:

Problem calling std::max

c++windowsvisual-c++bison

提问by Haiyang

I compiled my bison-generated files in Visual Studio and got these errors:

我在 Visual Studio 中编译了我的野牛生成的文件并得到了这些错误:

...\position.hh(83): error C2589: '(' : illegal token on right side of '::'
...\position.hh(83): error C2059: syntax error : '::'
...\position.hh(83): error C2589: '(' : illegal token on right side of '::'
...\position.hh(83): error C2059: syntax error : '::'

...\position.hh(83): 错误 C2589: '(' : '::' 右侧的非法标记
...\position.hh(83): 错误 C2059: 语法错误: '::'
。 ..\position.hh(83): 错误 C2589: '(' : '::' 右侧的非法标记
...\position.hh(83): 错误 C2059: 语法错误: '::'

The corresponding code is:

对应的代码是:

inline void columns (int count = 1)
{
  column = std::max (1u, column + count);
}

I think the problem is with std::max; if I change std::max to equivalent code then there is no problem anymore, but is there a better solution instead of changing the generated code?

我认为问题在于 std::max; 如果我将 std::max 更改为等效代码,那么就没有问题了,但是有没有更好的解决方案而不是更改生成的代码?

Here is the bison file I wrote:

这是我写的野牛文件:

//
// bison.yy
//

%skeleton "lalr1.cc"
%require "2.4.2"
%defines
%define parser_class_name "cmd_parser"
%locations
%debug
%error-verbose

%code requires {
class ParserDriver;
}

%parse-param { ParserDriver& driver }
%lex-param { ParserDriver& driver }

%union {
    struct ast *a;
    double d;
    struct symbol *s;   
    struct symlist *sl;
    int fn;         
}

%code {
#include "helper_func.h"
#include "ParserDriver.h"
std::string error_msg = "";
}

%token <d> NUMBER
%token <s> NAME
%token <fn> FUNC
%token EOL
%token IF THEN ELSE WHILE DO LET
%token SYM_TABLE_OVERFLOW
%token UNKNOWN_CHARACTER

%nonassoc <fn> CMP
%right '='
%left '+' '-'
%left '*' '/'
%nonassoc '|' UMINUS

%type <a> exp stmt list explist
%type <sl> symlist

%{
extern int yylex(yy::cmd_parser::semantic_type *yylval,
 yy::cmd_parser::location_type* yylloc);
%}

%start calclist
%%

... grammar rules ...

回答by James McNellis

You are probably including windows.hsomewhere, which defines macros named maxand min.

您可能包含windows.h某个地方,它定义了名为max和 的宏min

You can #define NOMINMAXbefore including windows.hto prevent it from defining those macros, or you can prevent macro invocation by using an extra set of parentheses:

您可以#define NOMINMAX在包含之前windows.h阻止它定义这些宏,或者您可以通过使用一组额外的括号来阻止宏调用:

column = (std::max)(1u, column + count);

回答by Rob Kennedy

Define the NOMINMAX symbol at the top of your source, before you include any headers. Visual C++ defines minand maxas macros somewhere in windows.h, and they interfere with your use of the corresponding standard functions.

在包含任何标题之前,在源顶部定义 NOMINMAX 符号。Visual C++ 将min和定义maxwindows.h 中某处的宏,它们会干扰您对相应标准函数的使用。

#define NOMINMAX