【问题标题】:Problem calling std::max调用 std::max 时出现问题
【发布时间】:2011-02-16 21:12:41
【问题描述】:

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

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

对应的代码是:

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

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

这是我写的野牛文件:

//
// 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 ...

【问题讨论】:

    标签: c++ windows visual-c++ bison


    【解决方案1】:

    您可能在某处包含windows.h,它定义了名为maxmin 的宏。

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

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

    【讨论】:

      【解决方案2】:

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

      #define NOMINMAX
      

      【讨论】:

        猜你喜欢
        • 2011-06-05
        • 2017-12-29
        • 1970-01-01
        • 2023-01-13
        • 2020-10-12
        • 1970-01-01
        • 2021-04-02
        • 2012-10-05
        • 2019-03-26
        相关资源
        最近更新 更多