0

Using MS VC++ 2012 and Boost library 1.51.0

This is a snapshot of my problem:

struct B {
    C* cPtr;
}

struct C {
    void callable (int);
}

void function (B* bPtr, int x) {
    // error [1] here
    boost::thread* thrPtr = new boost::thread(bPtr->cPtr->callable, x) 
    // error [2] here
    boost::thread* thrPtr = new boost::thread(&bPtr->cPtr->callable, x) 
}

[1] error C3867: 'C::callable': function call missing argument list; use '&C::callable' to create a pointer to member

[2] error C2276: '&' : illegal operation on bound member function expression

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Old newbie
  • 811
  • 2
  • 11
  • 21

1 Answers1

4

You want boost::thread* thrPtr = new boost::thread(&C::callable, bPtr->cPtr, x);. Here is a working example:

#include <sstream>
#include <boost/thread.hpp>
#include <boost/bind.hpp>


struct C {
    void callable (int j)
    { std::cout << "j = " << j << ", this = " << this << std::endl; }
};

struct B {
    C* cPtr;
};

int main(void)
{
    int x = 42;
    B* bPtr = new B;
    bPtr->cPtr = new C;

    std::cout << "cPtr = " << bPtr->cPtr << std::endl;;

    boost::thread* thrPtr = new boost::thread(&C::callable, bPtr->cPtr, x);
    thrPtr->join();
    delete thrPtr;
}

Sample output:

cPtr = 0x1a100f0
j = 42, this = 0x1a100f0
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 1
    `bind` can be dispensed with: `boost::thread(&C::callable, bPtr->cPtr, x)` would achieve the same. – Luc Danton Oct 08 '12 at 13:56
  • Thanks. You're right, answer updated. I'm not sure why I thought it was needed. (I think because it is needed if you need to convert a shared_ptr.) – David Schwartz Oct 08 '12 at 13:58