【问题标题】:error: expected '=', ',', ';', 'asm' or '__attribute__' before 'stackEmpty'错误:在 'stackEmpty' 之前应有 '='、','、';'、'asm' 或 '__attribute__'
【发布时间】:2026-02-11 04:00:01
【问题描述】:

此代码用于在 C 中实现通用堆栈功能。

Code for stack.h
-------------

    typedef struct{
    void *elements;
    int elementSize;
    int logofElementsLength;
    int allocatedLength;

}stack;

bool stackEmpty(const stack *s);

Client.c 中的实现代码

bool stackEmpty(const stack *s)
{return (s->logLength==0);
}

错误

error: expected '=', ',', ';', 'asm' or '__attribute__' before 'stackEmpty'

评论

否则代码编译,我只在这一行得到一个错误。显然错误必须来自这行代码。我正在使用

gcc -O0 -g3 -Wall -arch i386 -c -fmessage-length=0 -MMD -MP -MF"Client.d" -MT"Client.d" -o"Client.o" "../Client.c"

编译。

我在 MAC Snow Leopard 操作系统上运行。我在Client.c 中导入了stack.h,所有其他代码都可以编译并运行良好。任何帮助将不胜感激。

【问题讨论】:

    标签: c compiler-errors


    【解决方案1】:

    嗯,与 C++ 不同,bool 在 C 中不是有效类型(当然,除非使用 stdbool.h)。我见过 bool 在 C 中这样使用:

    typedef enum { false, true } bool;
    

    【讨论】:

    • 应该是Bool 而不是bool! Thu 不得用小写字母拼写人名。虽然为很棒的 typedef +1。
    • @WTP:类型名称应该是小写的。
    • 除非您的编码风格要求它们是 CamelCase(例如 Apple 风格)。
    • ...或者你的类型被称为FILE(或_Bool_Complex在C99中)。啧啧,那些讨厌的标准委员会!