c - C无法将函数指针类型识别为相同
问题描述
在最近的一个项目中,我在过去一个小时左右一直在努力修复这个语法错误;gcc 抱怨它所说的两个相同类型的对象之间的类型不匹配:
../../devices/timer.c: In function ‘timer_interrupt’:
../../devices/timer.c:181:19: warning: passing argument 1 of ‘thread_foreach’ from incompatible pointer type [-Wincompatible-pointer-types]
181 | thread_foreach (timer_check, NULL);
| ^~~~~~~~~~~
| |
| void (*)(struct thread *, void *)
In file included from ../../devices/timer.c:9:
../../threads/thread.h:134:42: note: expected ‘void (*)(struct thread *, void *)’ but argument is of type ‘void (*)(struct thread *, void *)’
134 | void thread_foreach (thread_action_func *func, void *aux);
| ~~~~~~~~~~~~~~~~~~~~^~~~
../../devices/timer.c: At top level:
../../devices/timer.c:185:1: error: conflicting types for ‘timer_check’
185 | timer_check (struct thread *thread, void *aux) {
| ^~~~~~~~~~~
In file included from ../../devices/timer.c:1:
../../devices/timer.h:29:6: note: previous declaration of ‘timer_check’ was here
29 | void timer_check(struct thread *thread, void *aux);
| ^~~~~~~~~~~
我玩过诸如添加/删除引用或取消引用运算符之类的事情,更改所涉及函数的一些签名以使它们与我在网上看到的示例更相似。
例如,我尝试更改thread_action_func
from typedef void thread_action_func
to的签名typedef void (*thread_action_func)
,并将函数参数中类型的使用从 to 更改thread_action_func *
为thread_action_func
,但它要么抱怨传递的类型不再是函数或函数指针,要么抛出关于相同的相同错误类型不匹配。
我还尝试在函数thread_foreach
调用的位置前面添加一个地址timer_check
作为参数,例如thread_foreach(&timer_check,...)
,但错误与最初相同。
相关的函数/原型/类型定义是:
线程.h:
struct thread
{
...
int64_t block_ticks;
...
};
typedef void thread_action_func (struct thread *t, void *aux);
void thread_foreach (thread_action_func *func, void *aux);
线程.c:
void
thread_foreach (thread_action_func *func, void *aux)
{
struct list_elem *e;
ASSERT (intr_get_level () == INTR_OFF);
for (e = list_begin (&all_list); e != list_end (&all_list);
e = list_next (e))
{
struct thread *t = list_entry (e, struct thread, allelem);
func (t, aux);
}
}
计时器.h:
void timer_check(struct thread *thread, void *aux);
计时器.c:
void
timer_sleep (int64_t ticks)
{
int64_t start = timer_ticks ();
ASSERT (intr_get_level () == INTR_ON);
struct thread *cur = thread_current();
cur->block_ticks = ticks;
enum intr_level old_level = intr_disable ();
thread_block();
intr_set_level (old_level);
thread_yield();
}
static void
timer_interrupt (struct intr_frame *args UNUSED)
{
ticks++;
thread_tick ();
thread_foreach (timer_check, NULL);
}
void
timer_check (struct thread *thread, void *aux) {
if (thread->status == THREAD_BLOCKED) {
thread->block_ticks--;
if (thread->block_ticks <= 0) {
thread_unblock(thread);
}
}
}
我通过搜索此类问题找到的所有结果都只是在一个方面或另一个方面相似,不够接近以至于没有用处,例如显示函数指针的示例是什么或指向整数或字符的指针的错误。
我猜这是一些明显的语法错误,我太沮丧了,没有注意到,但我现在无法真正清楚地看到可能导致问题的原因。
解决方案
struct
当您在不同的范围内重新定义标记时,会发生此错误。
您尚未提供Minimal Reproducible Example,因此我们无法明确确定错误所在,但可能是以下代码timer.h
:
void timer_check(struct thread *thread, void *aux);
struct thread
用函数原型范围声明。这仅在函数声明期间定义了一个struct
类型,这通常是无用的。
然后,在 中timer.c
,大概你包含了一些声明的头文件thread_unblock
,并且这个头文件被包含在timer.h
被包含之后(如果是的话)。该标头声明了它自己的struct thread
并在thread_unblock
.
由于该函数timer_check
采用的参数类型与 中使用的参数类型不同thread_unblock
,因此编译器报告它们具有不兼容的类型,即使它们具有相同的名称(来自不同的范围)。
要解决此问题,timer.h
应在声明struct thread
之前包含声明的标头timer_check
。然后参数声明struct thread *thread
将引用已经可见的结构类型,而不是定义一个新的。
为了说明,这里是重现错误的代码:
// struct thread; // Uncommenting this line will elimninate the error.
void foo(struct thread *);
void bar(void (*)(struct thread *));
void baz(void)
{
bar(&foo);
}
推荐阅读
- graphql - 如何使用 shopify_api gem 查询变量 Shopify API
- bash - `shopt -s inherit_errexit`在声明命令中无效
- vue.js - [Vue 警告]:在渲染期间访问了属性“$primevue”,但未在实例上定义
- css - 最新更新后,如何在 vuetify 中的 v-dialog 中添加自定义样式?
- sqlite - 使用 Sequelize 和 Sequelite 播种初始数据库:SQLITE_ERROR: no such table
- python - python - 了解 librosa.stft 输出形状
- angular - 在 ngIf else 模板中调用(单击)函数时出错
- c++ - 为什么 sizeof() 不打印 16 个字节而不是 8 个字节?
- authentication - 如何以编程方式使用 Symfony 登录用户?
- angular - SSR 节点服务器中的 http-proxy-middleware 不重定向本地请求