Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C &引用;参数的类型不完整";警告_C_Incomplete Type - Fatal编程技术网

C &引用;参数的类型不完整";警告

C &引用;参数的类型不完整";警告,c,incomplete-type,C,Incomplete Type,我将其保存在C文件中: struct T { int foo; }; C文件包含到h文件的以下行: typedef struct T T; void listInsertFirst(T data, int key, LinkedList* ListToInsertTo); 函数listInsertFirst就是我得到警告的那个函数。如何修复它?您确定这是问题的第一个参数吗?当然,可以尝试暂时将参数类型从T更改为int。第三个参数很可能就是问题所在 许多编译器没有很好地指出这类问题中的

我将其保存在C文件中:

struct T
{
    int foo;
};
C文件包含到h文件的以下行:

typedef struct T T;
void listInsertFirst(T data, int key, LinkedList* ListToInsertTo);

函数
listInsertFirst
就是我得到警告的那个函数。如何修复它?

您确定这是问题的第一个参数吗?当然,可以尝试暂时将参数类型从
T
更改为
int
。第三个参数很可能就是问题所在


许多编译器没有很好地指出这类问题中的问题。

尝试将结构定义移动到h文件中,在typedef之前。

当包含头文件时,编译器知道
t
是一个大小未知的结构,
listInsertFirst
想要一个作为其第一个参数。但是编译器无法安排对
listInsertFirst
的调用,因为它不知道为
t data
参数在堆栈上推送多少字节,
t
的大小只在定义了
listInsertFirst
的文件中知道

最好的解决方案是将
listInsertFirst
更改为将
T*
作为其第一个参数,这样您的头文件会这样说:

extern void listInsertFirst(T *data, int key, LinkedList* ListToInsertTo);

然后,您会得到一个用于
T
数据类型的不透明指针,由于所有指针的大小都相同(至少在现代世界是如此),编译器将知道如何在调用
listInsertFirst

时构建堆栈,正如我们在注释中发现的那样,问题是
struct
的定义出现在标题中
T
的定义之后。你在这里真的很落后。标题应该定义所有类型和函数原型,并且您的C文件应该使用它们

相反,您要做的是更改insert函数的签名,以接收指向数据和数据大小的指针。然后,您可以为数据分配一些内存,复制并存储数据。您不需要特定类型,只需将其声明为
void*

void listInsertFirst(void *data, size_t data_size, int key, LinkedList* ListToInsertTo);
然后调用方将执行以下操作:

struct T { int foo; };
struct T x = { ... };
int someKey = ...;
LinkedList *someList = ...;
listInsertFirst(&x, sizeof x, someKey, someList);
  • 在头文件中定义
    struct
    ,而不是在.c文件中
  • 为结构和类型定义选择不同的名称

  • 谢谢,如果有人包含h文件,我想让类型为known呢?您应该注意的一点是,您在这里按值传递一个
    struct
    。这几乎肯定是个坏主意……不管你为什么要在C文件而不是头文件中键入定义。另外,您可以通过使用struct{int foo;}T;编译器指的是什么参数?我实际上不确定在实现(任何类似字典的)DS时应该做什么:调用insert函数的人是否应该进行内存分配并传递指针,或者他应该传递对象,插入函数应该进行分配吗?问题是我有:typedef struct LinkedList{ListNode*head;ListNode*tail;}LinkedList;它使用类型“ListNode”,如果我把这个typedef结构放在h文件上,我必须添加“ListNode”的typedef结构,但是没有人应该知道这个类型…但是LinkedList必须知道ListNode。因此,如果有人知道LinkedList,那么它也必须知道ListNOde。您还可以添加另一个h文件:将ListNode添加到新的h文件中,并将其包含在原始h文件中