[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<std::pair<double, std::valarray<double> > >& h) where Obj3Type and Obj4Type are defined as: class Obj3Type { bool var1; unsigned int var2; unsigned int var3; double var4; double var5; MyType var6; boost::scoped_array<int> var7; boost::scoped_array<boost::scoped_array<double> > var8; }; struct Obj4Type { MyType var1; double var2; double var3; double var4; double var5; double var6; Vector var7; Vector var8; double var9; bool var10; } where MyType is an enumerated type and Vector is another struct of mine. (Where I have used structs rather than classes, this is for convenience.) Note that obj4 does not generate a compiler error, even when I move it to appear before obj3 in the parameter list. 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? Thanks for any help.

On 1/22/07, Paul Giaccone <paulg@cinesite.co.uk> wrote:
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 <paulg@cinesite.co.uk> 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