Search code examples
c++visual-c++c++11stdstd-function

std::function expression does not result in a function which accepts 1 Arguement


I am learning at using std::function to pass functions. I have taken the code code from MSDN Site which shows me how to get to know if a Process is started.

http://pastebin.com/gVkXZC9b

I know the function void signal_onProcessStart is not necessary I will enlarge it later.

My Problem is that my Compiler is telling me that there is an Error for std::function.

Expression does not result in a function which accepts 1 Arguement.

I am using the German Version of Visual Studio, so I translated it. I think the error is called differently in English.

All Functions which shall be triggered have void as "Return-Value" and HRESULT* as the only parameter.

Greetings

EDIT:

I have made a small example with the same error:

#include<functional>
#include<iostream>
#include<Windows.h>

using namespace std;

class workingClass
{
private:
    std::function<void(int)> f;
public:
    workingClass(std::function<void(int)> p)
    {
        this->f = p;
    }
    void triggerme(int x)
    {
        this->f(x);
    }
    ~workingClass(){}
};

class managingClass
{
private:
    function<void(int)> f;
    workingClass * ptr;
    int x;
protected:
    void trigger(int x)
    {
        this->f(x);
    }
public:

    managingClass(function<void(int)> f)
    {
        this->f = f;
        function<void(int)> tmp = bind(&managingClass::trigger, this->x);
        ptr = new workingClass(tmp);
        ptr->triggerme(20);
    }
    ~managingClass(){}
};

void triggered(int x)
{
    cout << "it is triggered";
}

int main()
{
    function<void(int)> t = bind(&triggered, 20);
    managingClass temp(t);
    system("pause");
}

ERROR LINE 1149 in the file functional Expression will not result in a function which accepts 1


Solution

  • You should use placeholders for your purpose. This will work:

    using std::placeholders::_1;
    
    class workingClass
    {
    private:
        std::function<void(int)> f;
    public:
        workingClass(std::function<void(int)> p)
        {
            this->f = p;
        }
        void triggerme(int x)
        {
            this->f(x);
        }
        ~workingClass(){}
    };
    
    class managingClass
    {
    private:
        function<void(int)> f;
        workingClass * ptr;
        int x;
    protected:
        void trigger(int x)
        {
            this->f(x);
        }
    public:
    
        managingClass(function<void(int)> f)
        {
            this->f = f;
            function<void(int)> tmp = bind(&managingClass::trigger, this, _1);//, this->x);
                                                                    //    ^^
            ptr = new workingClass(tmp);
            ptr->triggerme(20);
        }
        ~managingClass(){}
    };
    
    void triggered(int x)
    {
        cout << "it is triggered";
    }
    
    int main()
    {
        function<void(int)> t = bind(&triggered, _1);//, 20);
                                            //   ^^
        managingClass temp(t);
    }