2

I am trying to use boost::function with instance methods using the following example

class someclass
{
public:

    int DoIt(float f, std::string s1)
    {
        return 0;
    }

    int test(boost::function<int(float, std::string)> funct)
    {
         //Funct should be pointing to DoIt method here
         funct(12,"SomeStringToPass");
    }

    void caller()
    {
                test(DoIt); //Error : 'someclass::DoIt': function call missing argument list; use '&someclass::DoIt' to create a pointer to member
    }
};

Any suggestion on how I could resolve this issue ?

1
  • Use std::function and std::bind if you can... Commented Nov 15, 2013 at 1:52

1 Answer 1

2

You should use boost::bind:

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <string>
#include <iostream>

using namespace std;

class someclass
{
public:

    int DoIt(float f, std::string s1)
    {
        return 0;
    }

    int test(boost::function<int(float, std::string)> funct)
    {
        return funct(5.0, "hello");
    }

    void caller()
    {
        cout << test(boost::bind(&someclass::DoIt, this, _1, _2)) << endl;
    }
};

int main() {
    someclass s;
    s.caller();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that did it - Marked as answer after timer
No problem Rajeshwar, could you mark this answer as the correct one? It's the tick symbol on the left. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.