[bind] Problem compiling when passing instantiation of class
I am using Boost bind in conjunction with Boost unit test, as I have
many times before, but I can't get my code to compile. I've looked at
http://boost.org/libs/bind/bind.html#Troubleshooting but don't see
anything that covers my case.
Here is the line that is failing (with disguised variable names):
test_suite -> add(BOOST_TEST_CASE(boost::bind(my_test, obj1, obj2,
obj3, 1.0, obj4, 10, 1E-9, results)));
and here is the error that Microsoft Visual C++ .NET is generating:
main.cpp(152): error C2665: 'boost::bind' : none of the 2 overloads
can convert parameter 4 from type 'Obj3Type'
The variables obj3 and obj4 are of type Obj3Type and Obj4Type
respectively (see definitions below):
The function my_test is defined as:
void my_test(boost::shared_array<Vector>& a, boost::shared_array<Vector>& b,
Obj3Type& c,
const double d, const Obj4Type& e, const unsigned int f,
const double g, boost::shared_array
On 1/22/07, Paul Giaccone
I wondered if it was necessary to overload operator= for Obj3Type as, presumably, the compiler cannot generate it for the scoped arrays in that struct but can for everything in Obj4Type, including Vector, for which I have defined operator=. I wrote an operator= for Obj3Type, but it made no difference to the compiler error. This was to be expected really, as obj3 is passed by reference rather than by value.
Does it have anything to do with using structs instead of classes? I can't see how that could be the case.
So what am I missing here?
You are missing a copy constructor. Even though my_test() takes arguments by reference, Boost.Bind binds them by value and since you have scoped_array you need to provide a copy constructor (in addition to operator=) yourself. Or, you can bind by reference: boost::bind( ... , ref(obj3), ... ) hth -- Server Levent Yilmaz Mechanical Engineering @ PITT
Server Levent Yilmaz wrote:
On 1/22/07, Paul Giaccone
wrote: So what am I missing here?
You are missing a copy constructor. Even though my_test() takes arguments by reference, Boost.Bind binds them by value and since you have scoped_array you need to provide a copy constructor (in addition to operator=) yourself.
Thanks, that worked.
Or, you can bind by reference:
boost::bind( ... , ref(obj3), ... )
This didn't: the shared_arrays my object contained 10 objects when they were created but only 3 after being passed to the function. Not to worry anyhow, as the "proper" method of supplying and copy constructor and an assignment operator worked fine.
participants (2)
-
Paul Giaccone
-
Server Levent Yilmaz