错误:在受保护的'之前预期的不合格ID在代码中

时间:2014-04-27 09:50:48

标签: c++ c

我正在尝试自己编译cowpatty的一部分代码,但我的编译器总是偶然发现以下代码:

struct dot11hdr {
    union {
        struct {
            uint8_t     version:2;
            uint8_t     type:2;
            uint8_t     subtype:4;
            uint8_t     to_ds:1;
            uint8_t     from_ds:1;
            uint8_t     more_frag:1;
            uint8_t     retry:1;
            uint8_t     pwrmgmt:1;
            uint8_t     more_data:1;
            uint8_t     protected:1;
            uint8_t     order:1;
        } __attribute__ ((packed)) fc;

        uint16_t    fchdr;
    } u1;

    uint16_t    duration;
    uint8_t     addr1[6];
    uint8_t     addr2[6];
    uint8_t     addr3[6];

    union {
        struct {
            uint16_t    fragment:4;
            uint16_t    sequence:12;
        } __attribute__ ((packed)) seq;

        uint16_t    seqhdr;
    } u2;

} __attribute__ ((packed));

我在这里收到了行error: expected unqualified-id before 'protected'的错误uint8_t protected:1;。为什么?在使用给定的主文件编译完整代码时,一切正常。我已经在我自己的makefile中包含了每个cflag和ldflag,以防止与缺少cflags相关的错误。

2 个答案:

答案 0 :(得分:0)

Cowpatty是用C语言编写的,你需要编译它。正如评论中所指出的那样protected是C ++中的关键字,并将其编译为C ++代码无法正常工作。

检查编译器文档,看看你应该使用什么标志来编译为C(或使用像GCC这样的C编译器,错误表明你现在正在使用G ++)。

答案 1 :(得分:0)

您需要使用C编译器而不是C ++构建项目,否则您将进入惊喜世界。它们不再相互兼容,从这个意义上讲,protected是一个C ++关键字,它们从未出现过。

确保在编译时使用gcc或类似的C编译器而不使用 g ++或c ++运行时库。解决之后,编译器不会引发问题,因为protected不是C语言中的关键字。

一般来说,C项目应避免在C ++中使用关键字,甚至不要在标准库中使用类型,如字符串等等。

如果它不是一个公共API,在没有太大风险的情况下在内部重命名这个变量也没有风险,你可以说服上游将它重命名为更加C ++友好,但不能保证它会走得很远。 / p>