“错误:无效使用无效表达"当使用一个函数作为另一个的参数时 [英] "error: invalid use of void expression" when using a function as parameter of another

查看:69
本文介绍了“错误:无效使用无效表达"当使用一个函数作为另一个的参数时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编译时出现此错误:`错误:无效使用void表达式

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

我在倒数第二行收到此错误: queueDump(f,q,printToken(f,e)); 并且我不明白为什么.我正在尝试对函数 printToken 进行编码,该函数打印由 e

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);
}

推荐答案

当您传递结果的时,您的 queueDump 函数需要指向该函数的指针.执行.

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

printToken(f,e)使用参数 f e 执行函数 printToken .取而代之的是,您应该将3个参数传递给 queueDump - printToken f e queueDump 将使用这些参数执行实际执行.

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.

(在特定情况下, FILE * f c-> value 用作 prinToken 的参数,因此无需传递其他参数)

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

这篇关于“错误:无效使用无效表达"当使用一个函数作为另一个的参数时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆