【问题标题】:Allowing function overloading in C compiler (Suppressing "conflicting types for " Error in C) [duplicate]允许 C 编译器中的函数重载(抑制 C 中的“冲突类型”错误)[重复]
【发布时间】:2013-07-05 06:20:39
【问题描述】:

假设我的 C 代码在 C 中有两个不同的声明。

struct pcdata
{
    int x; int y;
};

int yyparse (struct pcdata *pp);
int yyparse (void *pp);

int yyparse (struct pcdata *pp)
{

}

用 cc/gcc 编译代码,出现conflicting types 错误。

test> cc -c hello.c
hello.c:7:5: error: conflicting types for 'yyparse'
int yyparse (void *pp);
    ^
hello.c:6:5: note: previous declaration is here
int yyparse (struct pcdata *pp);
^
1 error generated.
test> gcc -c hello.c
hello.c:7: error: conflicting types for ‘yyparse’
hello.c:6: error: previous declaration of ‘yyparse’ was here

g++ 没有错误,因为 c++ 允许函数重载。

test> g++ -c hello.c

是否有任何 gcc 选项允许函数重载?我有这个(简化的)代码和 Bison 2.7 生成的 tab.c 代码。

【问题讨论】:

  • 为什么yyparse()需要2个函数定义
  • 如果你想要函数重载的C++特性,使用C++编译代码。

标签: c gcc bison cc


【解决方案1】:

C 没有函数重载。

【讨论】:

    【解决方案2】:

    这在 C 中肯定行不通,但应该没有必要。

    bison 通常会创建一个yyparse,它根本不接受任何参数,但您可以使用%parse-param 声明(至少在不太旧的bison 版本中)告诉bison 您希望它接受哪些参数。

    在所有情况下,只有一个yyparse,除非您将两个单独的解析器编译到同一个程序中,当您有多个需要解析的语法时,当然会发生这种情况。在这种情况下,您可以使用%name-prefix 告诉bison,一个或两个bison 生成的解析器应该使用yy 以外的前缀。

    【讨论】:

    猜你喜欢
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多