在最近的一个项目中,我在过去一个小时左右一直在努力修复这个语法错误;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);
}
}
}
我通过搜索此类问题找到的所有结果都只是在一个方面或另一个方面相似,不够接近以至于没有用处,例如显示函数指针的示例是什么或指向整数或字符的指针的错误。
我猜这是一些明显的语法错误,我太沮丧了,没有注意到,但我现在无法真正清楚地看到可能导致问题的原因。