0
boost::function<void()> test_func;

struct test_t
{
   boost::function<void(int)> foo_;

   void test()
   {
      // This works as expected
      test_func = boost::bind(test_t::foo_, 1);
   }

};

int main(int argc, _TCHAR* argv[])
{
      test_t test;

      test.test();

      // error C2597: illegal reference to non-static member 'test_t::foo_'
      test_func = boost::bind(test_t::foo_, &test, 1);

      const auto a = 0;

   return 0;
}

What the problem with the code? Why code test_func = boost::bind(test_t::foo_, &test, 1); compiles in test_t::test() and gives me the error in main()?

Thank you

7.62
  • 3
  • 1
  • Take a look at http://stackoverflow.com/questions/2304203/how-to-use-boost-bind-with-a-member-function – R Sahu May 30 '14 at 20:44

2 Answers2

0

That's because inside a method test_t::foo_ refers to this->foo_, while test_t::foo_ in main could refer to foo_ only if it was a static member of that class. You need to write test.foo_ there instead.

polkovnikov.ph
  • 6,256
  • 6
  • 44
  • 79
0

The problem is more or less what the error says. test_t::foo_ is not a function; it's a functor (function object), and it's not static. As such, you cannot access it without a test_t object. Try this:

test_func = boost::bind(test.foo_, 1);

dlf
  • 9,045
  • 4
  • 32
  • 58