我有一个按钮类:
class Button : public Component {
private:
SDL_Rect box;
void* function;
public:
Button( int x, int y, int w, int h, void (*function)() );
~Button();
void handleEvents(SDL_Event event);
};
我想Button::function
在方法中执行Button::handleEvents
:
void Button::handleEvents(SDL_Event event) {
int x = 0, y = 0;
// If user clicked mouse
if( event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
// Get mouse offsets
x = event.button.x;
y = event.button.y;
// If mouse inside button
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
{
this->function();
return;
}
}
}
当我尝试编译时,出现以下错误:
Button.cpp: In the constructor ‘Button::Button(int, int, int, int, void (*)(), std::string)’:
Button.cpp:17:18: error: invalid conversion from ‘void (*)()’ to ‘void*’ [-fpermissive]
Button.cpp: In the function ‘virtual void Button::handleEvents(SDL_Event)’:
Button.cpp:45:19: error: can't use ‘((Button*)this)->Button::function’ as a function