我有一个用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){
}
最佳答案
您无法获得指向非静态成员函数的常规函数指针,因为通过该指针调用它的代码将无法指定要调用它的对象(基本上是this
的值)。
但是,您可以使用静态或非成员函数,并使用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_Memory
已经是Ili9341
的成员变量,则不需要。关于c++ - 引用C函数的C++方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32597621/