Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

c++ - Problem calling std::max

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

...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 : '::'

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?

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 ...
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are probably including windows.h somewhere, which defines macros named max and min.

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...