“error: invalid use of void expression” when using a function as parameter of another [duplicate]

不羁岁月 提交于 2019-12-11 15:29:02

问题


I get this error when trying to compile: `error: invalid use of void expression

I get this error at the penultimate line: queueDump(f, q, printToken(f, e)); and I don't understand why. I'm trying to code the function printToken that prints the token pointed by e

void queueDump(FILE *f, Queue *q, void(*dumpfunction)(FILE *f, void *e)) {
    fprintf(f, "(%d) --  ", q->size);
    for (InternalQueue *c=q->head; c != NULL; c = c->next)
        dumpfunction(f, c->value);
}

void printToken(FILE *f, void *e) {
    Queue *q;
    if (!f) {
        printf("Erreur d'ouverture du fichier\n");
        exit(1);
    }
    fprintf(f, "Infix : ");
    queueDump(f, q, printToken(f, e));
    fclose(f);
}

回答1:


Your queueDump function expects a pointer to a function while you pass the result of the execution to it.

printToken(f, e) executes function printToken with parameters f and e. Instead of this you should pass 3 parameters to queueDump - printToken, f, e; queueDump will use these parameters to perform the actual execution.

(in your specific case FILE *f and c->value are used as parameters to prinToken so no need to pass additional params)



来源:https://stackoverflow.com/questions/49475213/error-invalid-use-of-void-expression-when-using-a-function-as-parameter-of-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!