gpt4 book ai didi

C++ 方法对 C 函数的引用

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

我有一个用 C 语言定义的结构,它需要一个指向函数的指针。我在 C++ 中定义的函数/方法。如何将对 C++ 方法的引用传递给 C 结构?

这是我得到的错误:“类型为“void (Ili9341::)(void , int16_t, int16_t, uint16_t)”的值不能用于初始化类型为“void ()( void , int16_t, int16_t, uint16_t) C"

这是C文件

typedef struct Graphics_Display
{
int32_t size; //!< The size of this structure.
void *displayData; //!< A pointer to display driver-specific data.
uint16_t width; //!< The width of this display.
uint16_t heigth; //!< The height of this display.
void (*callPixelDraw)(void *displayData, int16_t x, int16_t y,
uint16_t value); //!< A pointer to the function to draw a pixel on this display.
void (*callPixelDrawMultiple)(void *displayData, int16_t x, int16_t y,
int16_t x0, int16_t count, int16_t bPP, const uint8_t *data,
const uint32_t *pucPalette); //!< A pointer to the function to draw multiple pixels on this display.
void (*callLineDrawH)(void *displayData, int16_t x1, int16_t x2, int16_t y,
uint16_t value); //!< A pointer to the function to draw a horizontal line on this display.
void (*callLineDrawV)(void *displayData, int16_t x, int16_t y1,
int16_t y2, uint16_t value); //!< A pointer to the function to draw a vertical line on this display.
void (*callRectFill)(void *displayData, const Graphics_Rectangle *rect,
uint16_t value); //!< A pointer to the function to draw a filled rectangle on this display.
uint32_t (*callColorTranslate)(void *displayData, uint32_t value); //!< A pointer to the function to translate 24-bit RGB colors to display-specific colors.
void (*callFlush)(void *displayData); //!< A pointer to the function to flush any cached drawing operations on this display.
void (*callClearDisplay)(void *displayData, uint16_t value); //!< A pointer to the function to clears Display. Contents of display buffer unmodified
} Graphics_Display;

这是 C++ 文件

    Ili9341::Ili9341(void){

}

Graphics_Display Ili9341::ili9341_GetDisplay(){
Graphics_Display g_sDriver =
{
sizeof(tDisplay),
ili9341_Memory,
#if defined(PORTRAIT) || defined(PORTRAIT_FLIP)
LCD_Y_SIZE,
LCD_X_SIZE,
#else
LCD_X_SIZE,
LCD_Y_SIZE,
#endif
ili9341_PixelDraw,
ili9341_PixelDrawMultiple,
ili9341_LineDrawH,
ili9341_LineDrawV,
ili9341_RectFill,
ili9341_ColorTranslate,
ili9341_Flush,
ili9341_ClearScreen
};
return g_sDriver;
}

void Ili9341::ili9341_PixelDraw(void *displayData, int16_t x, int16_t y, uint16_t value){

}

void Ili9341::ili9341_PixelDrawMultiple(void *displayData, int16_t x, int16_t y, int16_t x0, int16_t count, int16_t bPP, const uint8_t *data, const uint32_t *pucPalette){

}

void Ili9341::ili9341_LineDrawH(void *displayData, int16_t x1, int16_t x2, int16_t y, uint16_t value){

}

void Ili9341::ili9341_LineDrawV(void *displayData, int16_t x, int16_t y1, int16_t y2, uint16_t value){

}

void Ili9341::ili9341_RectFill(void *displayData, const Graphics_Rectangle *rect, uint16_t value){

}

uint32_t Ili9341::ili9341_ColorTranslate(void *displayData, uint32_t value){
return 0;
}

void Ili9341::ili9341_Flush(void *displayData){

}

void Ili9341::ili9341_ClearScreen(void *displayData, uint16_t value){

}

最佳答案

您无法获得指向非静态成员函数的常规函数​​指针,因为通过指针调用它的代码将无法指定要调用它的对象(基本上是 的值)这个)。

但是,您可以使用静态或非成员函数,并使用 displayData 字段来存储指向该对象的指针。像这样的事情:

void ili9341_PixelDrawStatic(void *displayData, int x, int y, int value) {
static_cast<Ili9341*>(displayData)->ili9341_PixelDraw(x, y, value);
}
void ili9341_PixelDrawMultipleStatic(void *displayData, int x, int y, int16_t count, int16_t bPP, const uint8_t *data, const uint32_t *pucPalette) {
static_cast<Ili9341*>(displayData)->ili9341_PixelDrawMultiple(x, y, count, bPP, data, pucPalette);
}
// ... and so on

Graphics_Display g_sDriver =
{
sizeof(tDisplay),
this,
// ...
ili9341_PixelDrawStatic,
ili9341_PixelDrawMultipleStatic,
// ... and so on
};

请注意,您现在无法使用 displayData 指向 ili9341_Memory - 但如果 ili9341_MemoryIli9341 的成员变量 已经有了,那么你就不需要它了。

关于C++ 方法对 C 函数的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32597621/

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