C typedef:参数类型不完整

时间:2021-07-17 22:21:18

GCC 3.4.5 (MinGW version) produces a warning: parameter has incomplete type for line 2 of the following C code:

GCC 3.4.5(MinGW版本)生成警告:参数具有以下C代码的第2行的不完整类型:

struct s;
typedef void (* func_t)(struct s _this);
struct s { func_t method; int dummy_member; };

Is there a way to fix this (or at least hide the warning) without changing the method argument's signature to (struct s *)?

有没有办法解决这个问题(或至少隐藏警告)而不将方法参数的签名更改为(struct s *)?

Note:
As to why something like this would be useful: I'm currently tinkering with an object-oriented framework; 'method' is an entry in a dispatch table and because of the particular design of the framework, it makes sense to pass '_this' by value and not by reference (as it is usually done)...

注意:至于为什么这样的东西会有用:我正在修补面向对象的框架; 'method'是调度表中的一个条目,由于框架的特殊设计,通过值传递'_this'而不是通过引用传递(通常是这样)...

5 个解决方案

#1


1  

You can't quite do this easily - according to the C99 standard, Section 6.7.5.3, paragraph 4:

根据C99标准第6.7.5.3节第4段,您不能轻易做到这一点:

After adjustment, the parameters in a parameter type list in a function declarator that is part of a definition of that function shall not have incomplete type.

调整后,作为该函数定义一部分的函数声明符中参数类型列表中的参数不应具有不完整类型。

Your options are, therefore, to have the function take a pointer to the structure, or to take a pointer to a function of a slightly different type, such as a function taking unspecified parameters:

因此,您的选择是让函数获取指向结构的指针,或者获取指向稍微不同类型的函数的指针,例如采用未指定参数的函数:

typedef void (* func_t)(struct s*);  // Pointer to struct
typedef void (* func_t)(void *);     // Eww - this is inferior to above option in every way
typedef void (* func_t)();           // Unspecified parameters

#2


1  

Switching to GCC 4 seems like it should work. MinGW version 4.3.0: http://sourceforge.net/project/showfiles.php?group_id=2435&package_id=241304&release_id=596917

切换到GCC 4似乎应该可行。 MinGW版本4.3.0:http://sourceforge.net/project/showfiles.php?group_id = 2435&package_id = 241304&release_id = 596917

#3


0  

Hiding warnings is generally pretty easy - just look at the help for your particular compiler.

隐藏警告通常非常简单 - 只需查看特定编译器的帮助。

http://developer.apple.com/documentation/DeveloperTools/gcc-4.0.1/gcc/index.html#//apple_ref/doc/uid/TP40001838

Note that suppressing warnings is generally not something I would advocate.

请注意,抑制警告通常不是我所倡导的。

#4


0  

The warning seems to be a bug with the current MinGW version of gcc. Contrary to what Adam said, it is valid C99 - section 6.7.5.3, paragraph 12 explicitly allows this:

该警告似乎是当前MinGW版本的gcc的一个错误。与亚当所说的相反,它是有效的C99 - 第6.7.5.3节,第12段明确允许:

If the function declarator is not part of a definition of that function, parameters may have incomplete type and may use the [*] notation in their sequences of declarator specifiers to specify variable length array types.

如果函数声明符不是该函数定义的一部分,则参数可能具有不完整的类型,并且可能在其声明符说明符序列中使用[*]表示法来指定可变长度数组类型。

There seems to be no way to instruct (this version of) gcc to not print this warning - at least I could not find a switch which worked - so I'm just ignoring it for now.

似乎没有办法指示(这个版本的)gcc不打印这个警告 - 至少我找不到一个工作的开关 - 所以我现在只是忽略它。

#5


-1  

You want to call it with a function pointer. Why not use a void pointer instead?

你想用函数指针调用它。为什么不使用void指针呢?

typedef void (*func_t)(void*);

You MIGHT be able to pass a loosely-typed function pointer as well; I don't have a compiler on hand.

你可能也可以传递一个松散类型的函数指针;我手头没有编译器。

typedef void (*func_t)(void (*)());

#1


1  

You can't quite do this easily - according to the C99 standard, Section 6.7.5.3, paragraph 4:

根据C99标准第6.7.5.3节第4段,您不能轻易做到这一点:

After adjustment, the parameters in a parameter type list in a function declarator that is part of a definition of that function shall not have incomplete type.

调整后,作为该函数定义一部分的函数声明符中参数类型列表中的参数不应具有不完整类型。

Your options are, therefore, to have the function take a pointer to the structure, or to take a pointer to a function of a slightly different type, such as a function taking unspecified parameters:

因此,您的选择是让函数获取指向结构的指针,或者获取指向稍微不同类型的函数的指针,例如采用未指定参数的函数:

typedef void (* func_t)(struct s*);  // Pointer to struct
typedef void (* func_t)(void *);     // Eww - this is inferior to above option in every way
typedef void (* func_t)();           // Unspecified parameters

#2


1  

Switching to GCC 4 seems like it should work. MinGW version 4.3.0: http://sourceforge.net/project/showfiles.php?group_id=2435&package_id=241304&release_id=596917

切换到GCC 4似乎应该可行。 MinGW版本4.3.0:http://sourceforge.net/project/showfiles.php?group_id = 2435&package_id = 241304&release_id = 596917

#3


0  

Hiding warnings is generally pretty easy - just look at the help for your particular compiler.

隐藏警告通常非常简单 - 只需查看特定编译器的帮助。

http://developer.apple.com/documentation/DeveloperTools/gcc-4.0.1/gcc/index.html#//apple_ref/doc/uid/TP40001838

Note that suppressing warnings is generally not something I would advocate.

请注意,抑制警告通常不是我所倡导的。

#4


0  

The warning seems to be a bug with the current MinGW version of gcc. Contrary to what Adam said, it is valid C99 - section 6.7.5.3, paragraph 12 explicitly allows this:

该警告似乎是当前MinGW版本的gcc的一个错误。与亚当所说的相反,它是有效的C99 - 第6.7.5.3节,第12段明确允许:

If the function declarator is not part of a definition of that function, parameters may have incomplete type and may use the [*] notation in their sequences of declarator specifiers to specify variable length array types.

如果函数声明符不是该函数定义的一部分,则参数可能具有不完整的类型,并且可能在其声明符说明符序列中使用[*]表示法来指定可变长度数组类型。

There seems to be no way to instruct (this version of) gcc to not print this warning - at least I could not find a switch which worked - so I'm just ignoring it for now.

似乎没有办法指示(这个版本的)gcc不打印这个警告 - 至少我找不到一个工作的开关 - 所以我现在只是忽略它。

#5


-1  

You want to call it with a function pointer. Why not use a void pointer instead?

你想用函数指针调用它。为什么不使用void指针呢?

typedef void (*func_t)(void*);

You MIGHT be able to pass a loosely-typed function pointer as well; I don't have a compiler on hand.

你可能也可以传递一个松散类型的函数指针;我手头没有编译器。

typedef void (*func_t)(void (*)());