2

I was trying to assigna function to a button with a method I have tried before, that also worked before, but for some reason I am getting this from my compiler now:

error C3867: 'WindowReq::calc_cb': non-standard syntax; use '&' to create a pointer to member

Here is the code:

class WindowReq : public Fl_Window
{
public:
    WindowReq(int W, int H, const char* Title);

    Fl_Input*   InVel;
    Fl_Input*   InDeg;
    Fl_Button*  Calc;


private:
    void calc_cb(Fl_Widget* o, void* v);

};



WindowReq::WindowReq(int W, int H, const char* Title) : Fl_Window(W, H, Title)
{
    begin();
        Calc = new Fl_Button((WINSIZE - 150), 50, 100, 30, "Calculate path");
        Calc->callback(calc_cb, this);

        InVel = new Fl_Input(70, 50, 100, 30, "Velocity:");

        InDeg = new Fl_Input(((WINSIZE / 3) + 50), 50, 100, 30, "Angle:");
    end();


    show();
}

I have no idea what the problem is and any help would be greatly appreciated.

Edit: This is the line causing the error according to VisualStudio:

Calc->callback(calc_cb, this);
Neko
  • 45
  • 5
  • *this* refers to the class not the class method. So you should find some other way to do it. – Sreekar Dec 22 '15 at 18:24
  • It is supossed to point to the class so I can let the widgets in it communicate without much effort. Regardless of that, taking the "this" out doesn't do anything to make it compile. – Neko Dec 22 '15 at 18:35

1 Answers1

0

You can't call instanced method like this.
The function has to be not dependent on an instance of a class.

So, basically you have multiple options how to get around this, but since you're passing this as the second parameter I guess that you have just forgot static keyword before calc_cb's declaration.

Then the call should look like:

Calc->callback(&WindowReq::calc_cb, this);

The other ways include using templated callbacks or C++11's lambdas that can call the method directly within the instanced class. There are probably even more ways to achieve that.

Zaffy
  • 16,801
  • 8
  • 50
  • 77
  • I'm afraid I don't understand. I haven't done much in C++ yet (learned C first, C++ is the second language I learn). Why should I declare `calc_cb` as static and what does `&WindowReq` even do? – Neko Dec 22 '15 at 19:15
  • @Neko I'd love to explain this to you but I'm worried it's too broad to get explained in comments. Instead search the internet for 'c++ static member function' and the ampersand tells compiler to take address of the expression right from it, in short - `&WindowReq::calc_cb` takes address of method `calc_cb` in class `WindowReq`. – Zaffy Dec 22 '15 at 20:10
  • I'll follow your advice to google, but your advice to declare `calc_cb` as static and to rewrite that line as you recommended only increased the amount of errors I got. Rather than how to fix it, maybe the better question is why it doesn't work in the first place. it does here with a similar example: [link](http://www3.telus.net/public/robark/) – Neko Dec 22 '15 at 20:31
  • @Neko The snippet you posted works for me. What are the errors? – Zaffy Dec 22 '15 at 20:42
  • "error C2227: left of '->value' must point to class/struct/union/generic type" I Just noticed that the new errors are now in the definiton of `calc_cb`. – Neko Dec 22 '15 at 21:06
  • Solved it. Thanks for your help. I Still don't know why it didn't work before though. Or why it works now for that matter. – Neko Dec 22 '15 at 21:11
  • @Neko Again there's a lot of explaining but this may help you - http://stackoverflow.com/q/2400690/823738 – Zaffy Dec 22 '15 at 21:33
  • Very enlightening. Thanks again. – Neko Dec 25 '15 at 01:29