“参数具有不完整的类型";警告 [英] "parameter has incomplete type" warning

查看:137
本文介绍了“参数具有不完整的类型";警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C文件中有此文件

struct T
{
    int foo;
};

C文件包含一个包含到以下行的h文件:

the C file has an include to an h file with those lines:

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

函数listInsertFirst是我得到警告的函数.我该如何解决?

the function listInsertFirst is the one I'm getting the warning on. How can I fix it?

推荐答案

正如我们在注释中发现的那样,问题在于struct T的定义出现在标题中的T定义之后.您这里确实倒退了.标头应定义所有类型和函数原型,并且您的C文件应使用它们.

As we've found out in the comments, the problem was that the definition of struct T occurred after the definition of T in the header. You really have things backwards here. The header should be defining all the types and function prototypes and your C files should be using them.

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

What you want to be doing instead is change the signature of your insert function to receive a pointer to your data and the size of the data. Then you can allocate some memory for the data, copy it and store it. You don't need a specific type, just declare it a void *.

void listInsertFirst(void *data, size_t data_size, int key, LinkedList* ListToInsertTo);

然后,呼叫者将执行以下操作:

Then the caller would do something like this:

struct T { int foo; };
struct T x = { ... };
int someKey = ...;
LinkedList *someList = ...;
listInsertFirst(&x, sizeof x, someKey, someList);

这篇关于“参数具有不完整的类型";警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆