【问题标题】:parameter has incomplete type in C参数在 C 中的类型不完整
【发布时间】:2012-11-16 23:24:12
【问题描述】:

我正在编写一些代码,当我尝试测试我的代码时,我得到了一个错误。

这是我的代码:

#include <stdio.h>

enum { add = 0, addu, sub, subu } mips_opcode;
typedef enum mips_opcode mips_opcode_t;

typedef unsigned char byte; // 8-bit int

struct mips {
    char *name;
    byte opcode;
};
typedef struct mips mips_t;

void init (mips_t *out, char *name_tmp, mips_opcode_t opcode_tmp) {
    out->name = name_tmp;
    out->opcode = (byte)opcode_tmp;
}

int main (void) {
    pritnf("no error i assume\n");

    return 0;
}

命令行中的错误是:

main.c:14:55: error: parameter 3 ('opcode_tmp') has incomplete type

我不能使用枚举作为参数还是我在这里做错了什么?

【问题讨论】:

  • 我很惊讶它没有窒息而走到这一步——它应该在typedef enum mips_opcode mips_opcode_t 行上给你一个错误,因为mips_opcode 是一个变量,而不是一个类型。
  • 我认为pritnf("no error") 是这里真正的笑话......
  • 看来您混淆了 C 和 c++。在 C 中,结构定义不是类型定义。
  • @wildplasser:在 C++ 中,结构定义也不是 typedef。
  • 要扩展 Kerrek 的评论,请参阅 this Dr. Dobb's article。在几乎所有情况下,struct mips 等同于 C++ 中的 mips,除非您有名称隐藏(这通常是不好的做法)。

标签: c enums compiler-errors incomplete-type


【解决方案1】:

这行是罪魁祸首:

enum { add = 0, addu, sub, subu } mips_opcode;

您正在声明一个名为 mips_opcode 的变量,它属于匿名 enum 类型。

应该是:

enum mips_opcode { add = 0, addu, sub, subu };

枚举列表的名称紧跟在单词enum 之后。

【讨论】:

    【解决方案2】:

    应该是这样的:

    enum mips_opcode { add = 0, addu, sub, subu }; // type name is "enum mips_opcode"
    typedef enum mips_opcode mips_opcode_t;        // type alias
    

    甚至:

    typedef enum { add = 0, addu, sub, subu } mips_opcode_t; // alias of anon. type
    

    不要混淆类型名称和变量!

    (顺便说一句,Posix 为类型保留_t 后缀,我相信...)

    【讨论】:

    • 非常感谢它起作用了,下次我会确保更好地区分变量名称和类型
    • 是的,POSIX 在任何头文件中保留所有以_t 结尾的符号,参见2.2.2 The Name Space 中的图表。
    • 我遇到了同样的错误,这是一个小错字,我没有注意到即使在尝试了上述操作后仍看到错误,请确保检查拼写两次
    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 2013-09-15
    • 2017-04-14
    • 2011-08-25
    • 2016-10-06
    • 2012-11-18
    • 1970-01-01
    • 2012-08-19
    相关资源
    最近更新 更多