0

I have a Button class, and I'm trying to add a callback function to it. Following the accepted answer to this question, here's the layout of my classes:

class Button {
public:
    void SetCallbackFunc(std::function<void()> const& f) {
        callbackFunc = f;
    }
private:
    std::function<void()> callbackFunc;
};

class SomeOtherClass {
public:
    void DoThings();
    void SetupButtonFunctionality() {
        Button *b = new Button();
        b->SetCallbackFunc(std::bind(&SomeOtherClass::DoThings, this, std::placeholders::_1));
    }
};

And these are all the compilation errors I'm getting:

error C2977: 'std::add_reference' : too many template arguments
error C2955: 'std::add_reference' : use of class template requires template argument list 
error C2146: syntax error : missing ',' before identifier 'type' (Content\SomeOtherClass.cpp)
error C2065: 'type' : undeclared identifier (Content\SomeOtherClass.cpp)
error C2064: term does not evaluate to a function taking 2 arguments (Content\SomeOtherClass.cpp)
error C2064: term does not evaluate to a function taking 0 arguments (GUI\Button.cpp)
error C2027: use of undefined type 'std::tuple_element<0,_Ftuple>'

What's wrong with the code? Is there another simple way to write what I want?

Community
  • 1
  • 1
idlackage
  • 2,715
  • 8
  • 31
  • 52

1 Answers1

2

The function does not take in a parameter so you don't need the placeholder parameter.

b->SetCallbackFunc(std::bind(&SomeOtherClass::DoThings, this));
clcto
  • 9,530
  • 20
  • 42
  • That makes a lot of sense, I've been misunderstanding that parameter, thanks. However, I still get this error after removing it: `error C2064: term does not evaluate to a function taking 0 arguments` – idlackage Oct 29 '14 at 20:46
  • 1
    @idlackage That error isn't being generated by this code. Somewhere you're using the call operator on something that isn't a function (I believe). – David G Oct 29 '14 at 20:50
  • 1
    See the documentation for [`C2064`](http://msdn.microsoft.com/en-us/library/z72c1dfd.aspx) for more information. – clcto Oct 29 '14 at 20:52
  • Apparently the error occurred because I was setting `callbackFunc` to `NULL` (0) instead of `nullptr` in some cases. Thanks for the help! – idlackage Oct 29 '14 at 21:34