简体   繁体   English

C:类型struct的定义不完整

[英]C: Incomplete definition of type struct

this error seems very easy to fix but i've been trying and have no clue. 这个错误似乎很容易解决,但是我一直在尝试并且没有任何线索。

So i have three files: 所以我有三个文件:

symtable.h: symtable.h:

typedef struct symbolTable *SymTable_T;

symtablelist.c: symtablelist.c:

#include "symtable.h"

struct Node{
    char* key;
    void* value;
    struct Node* next;
};

struct symbolTable{
    struct Node* head;
    int length;
};

SymTable_T SymTable_new(void){

/* code */

}

And main.c: 和main.c:

#include "symtable.h"

int main(int argc, const char * argv[]) {
    // insert code here...
    SymTable_T emptyTable = SymTable_new();

    emptyTable->length = 3;   <------- ERROR

    return 0;
}

I'm getting error: Incomplete definition of type "struct symbolTable" 我收到错误消息:“结构symbolTable”类型的定义不完整

Can anyone please give me a hint? 谁能给我一个提示吗?

The reason i declare my struct in my source file is that i will have another implementation for the header file. 我在源文件中声明我的结构的原因是我将对头文件进行另一种实现。 so is there another way to fix my bug beside moving my struct declaration? 那么除了移动结构声明外,还有另一种方法可以修复我的错误吗?

You can't access the members directly with an opaque pointer - if you keep the implementation in a separate source file, you'll have to access all the members via your interface, and not directly mess with the struct. 您不能使用不透明的指针直接访问成员-如果将实现保存在单独的源文件中,则必须通过界面访问所有成员,而不能直接与struct混淆。

For instance, add this to symtable.h : 例如,将其添加到symtable.h

void SymTable_set_length(SymTable_T table, int len);

this to symtablelist.c : 这到symtablelist.c

void SymTable_set_length(SymTable_T table, int len)
{
    table->length = len;
}

and in main.c change this: 并在main.c对此进行更改:

emptyTable->length = 3;

to this: 对此:

SymTable_set_length(emptyTable, 3);

although in this specific case passing the length as an argument to SymTable_new() is an obviously superior solution. 尽管在这种特定情况下,将长度作为参数传递给SymTable_new()显然是一种更好的解决方案。 Even more superior is not letting the user set the length of a linked list data structure at all - the length is the number of items in it, and it is what it is. 更优越的是根本不让用户设置链接列表数据结构的长度-长度是其中的项目数,也就是它的长度。 It would make no sense to, for instance, add three items to the list, and then allow main.c to set the length to 2 . 例如,将三个项目添加到列表中,然后允许main.c将长度设置为2 symtablelist.c can calculate and store the length privately, and main.c can find out what the length is, but it doesn't make much sense for main.c to be able to set the length directly. symtablelist.c可以私下计算和存储长度,而main.c可以找出长度,但是main.c能够直接设置长度没有多大意义。 Indeed, the whole point of hiding the members of a struct behind an opaque pointer like this is precisely to prevent client code from being able to mess with the data like that and breaking the data structure's invariants in this manner. 确实,将结构的成员隐藏在这样的不透明指针后面的全部目的是为了防止客户端代码像这样混乱数据并破坏数据结构的不变式。

If you want to access the members directly in main.c , then you have to have the struct definition visible, there is no alternative. 如果要直接在main.c访问成员,则必须使结构定义可见,没有其他选择。 This will mean either putting the struct definition in the header file (recommended) or duplicating it in main.c (highly unrecommended). 这意味着将结构定义放在头文件中(推荐)或将其复制到main.c (高度推荐)。

In typedef symbolTable *SymTable_T; typedef symbolTable *SymTable_T; , you refer to a non-existent type symbolTable . 时,您引用的symbolTable类型不存在。 In C (unlike C++) the type is named struct symbolTable . 在C中(与C ++不同),该类型名为struct symbolTable (Note: the question has changed to fix this since answering it.) (注意:自回答以来,问题已更改为解决此问题。)

There's a second problem. 还有第二个问题。 In main.c the code will need to be able to see the definition of struct symbolTable for you to be able to refer to fields of emptyTable . main.c ,代码将需要能够看到struct symbolTable的定义,以便您能够引用emptyTable字段。 At the moment, the definition is hidden in a .c file... it should be moved to the header. 目前,该定义隐藏在.c文件中...应将其移至标头。

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

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