VS 2015和FLTK回调问题

时间:2016-02-05 13:48:23

标签: c++ callback visual-studio-2015 fltk

我正在尝试移动我的FLTK项目并在VS 2015社区版下进行编译。这样做,我收到错误。我有一个如下代码:

#include <Fl/....>
....
class CWindow
{
private:
    ....
    Fl_Input *_textInputEditor;
    ....
    void _cbTextInput(Fl_Widget *refObject, void *objData)
    {
        // Do something when callback is triggered.
    }
public:
....
    void createWindow()
    {
        ....
        _textInputEditor = new Fl_Input(....);
        _textInputEditor->when(FL_WHEN_ENTER_KEY);
        _textInputEditor->callback((Fl_Callback*)&CWindow::_cbTextInput, this);
        ....

当我尝试编译时,出现错误:

Error C2440 'type cast': cannot convert from 'void (__thiscall CWindow::* )(Fl_Widget *,void *)' to 'Fl_Callback (__cdecl *)

这个相同的代码在Win 7下与MinGW 5.x完美编译(IDE:C :: B)。

有人可以帮助我吗?我想回调一下我的CWindow类的私有方法。

1 个答案:

答案 0 :(得分:0)

签名不正确。 _cbTextInput应该是静态的。那么问题就是访问成员变量。

static void _cbTextInput(Fl_Widget *refObject, void *objData)
{
    // No access to member variables so cast it to a CWindow* to get
    // at the member variables
    CWindow* self = (CWindow*) objData;
    self->cbTextInput();

    // Alternatively, if you do not wish to write another routine,
    // you will need to put self-> in front of every member variable
    // you wish to access.  Just personal preference: some people prefer
    // it that way.
}

void cbTextInput()
{
    // This has access to member variables
    ...
}