简体   繁体   English

错误:在 c 中重新定义 typedef

[英]error: redefinition of typedef in c

We have an assignment in school where we got a header file and we need to implement it.我们在学校有一个作业,我们有一个 header 文件,我们需要实现它。 The header defines: header 定义:

typedef struct Board* BoardP;

which to my understanding means the BoardP is a pointer to struct Board.据我了解,这意味着 BoardP 是指向 struct Board 的指针。 Anyhow, my implementation is:无论如何,我的实现是:

typedef struct Board
{
    int width;
    int height;
    char *board;
} *BoardP;

But i keep getting:但我不断得到:

Board.c:21: error: redefinition of typedef ‘BoardP’
Board.h:4: note: previous declaration of ‘BoardP’ was here

Any ideas as to why this happens?关于为什么会发生这种情况的任何想法? Thanks!谢谢!

EDIT: another question.编辑:另一个问题。 As you can see my struct hold an array of characters.如您所见,我的结构包含一个字符数组。 When I write a constructor should I initialize (malloc(sizeof(height*width)) the array first and then the struct? And how about use of free()? Should I free the array first and then the struct? Thanks当我写一个构造函数时,我应该先初始化 (malloc(sizeof(height*width)) 数组然后再初始化结构吗?那么使用 free() 怎么样?我应该先释放数组然后再释放结构吗?谢谢

Drop the typedef from the definition.从定义中删除typedef

struct Board
{
    int width;
    int height;
    char *board;
};

A struct is a struct;一个结构就是一个结构; it's not like you always have to typedef it.它不像你总是必须typedef它。 To be honest, I only typedef structs for opaque pointers .老实说,我只为不透明的指针typedef structs

From style(9) :style(9)

Avoid using typedefs for structure types.避免对结构类型使用 typedef。 Typedefs are problematic because they do not properly hide their underlying type; Typedef 是有问题的,因为它们没有正确隐藏其底层类型; for example you need to know if the typedef is the structure itself or a pointer to the structure.例如,您需要知道 typedef 是结构本身还是指向结构的指针。 In addition they must be declared exactly once , whereas an incomplete structure type can be mentioned as many times as necessary.此外,它们必须只声明一次,而不完整的结构类型可以根据需要多次提及。

It happens because in your pre-written header file you have to write an implementation for, you already typedef the data type struct Board * into a "new" data type BoardP .发生这种情况是因为在您预先编写的 header 文件中,您必须为其编写一个实现,您已经将数据类型struct Board *输入定义为“新”数据类型BoardP Your explanation that the BoardP is the pointer to the data type struct Board is correct.您对BoardP是指向数据类型struct Board的指针的解释是正确的。

But, to make the code in the header work, you only have to define what's missing, which is, in this instance, only an implementation of the struct Board data type.但是,要使 header 中的代码正常工作,您只需定义缺少的内容,在这种情况下,仅是struct Board数据类型的实现。

Your proposed solution would work if you didn't already have the typedef in the header file.如果您在 header 文件中还没有 typedef,那么您提出的解决方案将起作用。

As you already have it in the header file, only the code proposed by cnicutar (the code without the typedef ) should be put in the .c file.由于您已经在 header 文件中拥有它,因此只有 cnicutar 提出的代码(没有typedef的代码)应该放在.c文件中。

Regarding your second question...关于你的第二个问题...

You can malloc in either order you like, but I generally allocate the object itself before I try to allocate any sub objects, because that way you have a clear place to store the results of subobject malloc s (in the struct) rather than having to store them in otherwise useless temporary variables.您可以按您喜欢的任何顺序malloc ,但我通常在尝试分配任何子对象之前分配 object 本身,因为这样您就有一个清晰的位置来存储子对象malloc的结果而不是在结构中将它们存储在其他无用的临时变量中。

Be sure to handle your errors correctly - if the second malloc fails, you'll need to free the first one.确保正确处理您的错误 - 如果第二个malloc失败,您需要free第一个。 This is actually a place that many C programmers like to use goto :这其实是很多 C 程序员喜欢使用goto的地方:

struct Board *makeBoard(...)
{
    struct Board *b = malloc(sizeof *b);
    if(b == NULL) goto end1;

    b->board = malloc(...);
    if(b->board == NULL) goto end2;

    // ...

  end2:
    free(b);
  end1:
    return NULL;
}

When you free the data, you have to free the subobjects before you free the main objects, unless you store pointers to all the subobjects in temporary variables before you free the main object. free数据时,必须在free主对象之前释放子对象,除非在free主 object 之前将指向所有子对象的指针存储在临时变量中。 That is, the following won't work:也就是说,以下内容不起作用:

free(b);
free(b->board);

Because by the second statement, b is free d and won't be able to be safely dereferenced.因为通过第二条语句, bfree的 d 并且无法安全地取消引用。 It's cleanest just to do it in the opposite order.以相反的顺序执行它是最干净的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM