gpt4 book ai didi

c - 如何为 cairo_pdf_surface_create_for_stream() 创建正确的回调?

转载 作者:行者123 更新时间:2023-11-30 18:06:45 25 4
gpt4 key购买 nike

我需要将 PDF 输出到文件描述符,我正在考虑使用 Cairo 和 cairo_pdf_surface_create_for_stream():

我有一个写入函数,具有以下签名:

static cairo_status_t cairowrite(void *fp, unsigned char const *data, unsigned int length);

它返回CAIRO_STATUS_SUCCESS

现在,我如何将此函数作为回调传递?

我尝试过这个:

cairo_write_func_t (*wfunc)(void *, unsigned char const *, unsigned int);
wfunc = (cairo_write_func_t)&cairowrite;
surface = cairo_pdf_surface_create_for_stream((cairo_write_func_t)wfunc, fp, (double)realwidth, (double)realwidth);

但是我收到编译器警告:警告:来自不兼容的指针类型的赋值这位于我执行wfunc = &cairowrite;的行上。

我做错了什么?

回调函数给出了此 gcc 错误:cairo_status_t,但必须是 cairo_write_func_t 类型。

使用 clang 我收到此错误:

qrencode.c:250:11: warning: incompatible pointer types assigning to 'cairo_write_func_t (*)(void *, unsigned char const *, unsigned int)' from
'cairo_write_func_t' (aka 'cairo_status_t (*)(void *, unsigned char const *, unsigned int)')
wfunc = (cairo_write_func_t)&cairowrite;
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.

我应该做什么?

最佳答案

您可以尝试使用定义函数指针

cairo_status_t (*wfunc)(FILE *, unsigned char *, unsigned int) = &cairowrite;

示例:

#include <iostream>

int funp(int a, int b) {
return a + b;
}

int call(int a, int b, int (*ptr)(int, int)) {
int result = ptr(a, b);
return result;
}

int main() {
int (*ptr)(int, int) = &funp;
int r = call(3, 5, ptr);
std::cout << "Result " << r << std::endl;
return 0;
}

输出:

Result 8

关于c - 如何为 cairo_pdf_surface_create_for_stream() 创建正确的回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5321499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com