调用std::max函数时出现问题

79

我在Visual Studio中编译我的bison生成文件,出现了以下错误:

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

对应的代码如下:

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

我认为问题出在 std::max 上;如果我将 std::max 更改为等效代码,则不再有问题,但是是否有更好的解决方案可以避免更改生成的代码?

这是我编写的 bison 文件:

//
// 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 ...
2个回答

162

你可能在某处包含了windows.h,该文件定义了名为maxmin的宏。

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

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

28

在包含任何头文件之前,在你的源代码顶部定义NOMINMAX符号。Visual C++在windows.h中定义minmax作为宏,它们会干扰你使用对应的标准函数。

#define NOMINMAX

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接