"void" 类型的值不能分配给“void(*)(struct *Queue, int)”类型的实体

a value of type "void" cannot be assigned to an entity of type "void(*)(struct *Queue, int)

我有以下结构:

typedef struct{
    int *arr;
    int maxSize, curSize;
    int first, last;
    int(*isEmptyFunc)(Queue);
    int(*isFullFunc)(Queue);
    void(*EnqueueFunc)(struct Queue*, int);
    void(*DequeueFunc)(struct Queue*);
    int(*TopFunc)(Queue);
} Queue;

以及 returns 指向新队列的指针的创建队列函数:

    int *arr = malloc(sizeof(int) * size);
    isNull(arr);
    Queue *q = malloc(sizeof(Queue));
    isNull(q);

当我尝试为函数指针赋值时,我会: (这一切都发生在 CreateQueue 函数中)

q->isEmptyFunc = isEmpty(*q);
q->isFullFunc = isFull(*q);
q->TopFunc = Top(*q);
q->DequeueFunc = Dequeue(q);

实际函数在我包含在 .c 文件顶部的头文件中声明,并在 CreateQueue 函数下方实现。 前三个赋值似乎没问题,但对于第四个,编译器尖叫:

IntelliSense: a value of type "void" cannot be assigned to an entity of type "void (*)(struct Queue *)" 

出队函数实现为:

void Dequeue(Queue *q) {
    if (q->isEmptyFunc()) return;
    q->first = (q->first + 1) % (q->maxSize);
    q->curSize--;
}

这是怎么回事?

这里的主要问题是,isEmptyFuncisFullFuncEnqueueFuncDequeueFunc,都是函数指针。您正在尝试放置一个函数调用的 return 值(这里,我们可以假设这不是 函数指针 )。那是完全错误的。 不行,你不应该那样做。

现在,如果我们看到你的情况

The first three assignments seem to be fine,

编译器不会在这里报错,因为三个函数都调用return一些值(可能是int?)并且隐式转换为函数指针 类型,但是,行为未定义。你不能那样做。

but for the fourth the compiler screams:

在那种情况下,Dequeue()函数return类型是void,不能用作。因此,编译器(谢天谢地)抱怨。

TL;DR 您需要更改 所有 以上语句。