T
Tigera
Greetings,
I am trying to learn to use the Boost library, and I've run into a
frustrating problem with the line marked here:
#include <boost/function.hpp>
#include <boost/functional.hpp>
#include <boost/bind.hpp>
#include <iostream>
using std::cout;
using std::endl;
class A { };
class B { };
class C { };
class D { };
class Visitor
{
public:
boost::function<float (A first)> foo;
};
class VisitorAB
{
public:
float operator()(A first, B second) { return 0.0; }
};
class VisitorAC
{
public:
float operator()(A first, B second) { return 1.0; }
float operator()(A first, C second) { return 2.0; }
float bar(A first, C second) { return 3.0; }
};
int main()
{
VisitorAB vab;
VisitorAC vac;
Visitor v;
A a;
B b;
C c;
v.foo = boost::bind2nd(vac, c); //Gives me a compile error
return 0;
}
I've also tried these:
v.foo = boost::bind(vac, _1, c);
v.foo = boost::bind<float (a)>(vac, _1, c);
v.foo = boost::bind( boost::mem_fun_ref(&VisitorAC::bar), c);
I finally found after about three hours that if I used v.foo =
boost::bind<float>(vac, _1, c) that I could cajole the program into
compiling.
I'm confused on the use of bind. What could I have done to make this
work with boost::bind2nd? Why is it that boost::bind only works when
you specify a template parameter?
Your answers are appreciated.
I am trying to learn to use the Boost library, and I've run into a
frustrating problem with the line marked here:
#include <boost/function.hpp>
#include <boost/functional.hpp>
#include <boost/bind.hpp>
#include <iostream>
using std::cout;
using std::endl;
class A { };
class B { };
class C { };
class D { };
class Visitor
{
public:
boost::function<float (A first)> foo;
};
class VisitorAB
{
public:
float operator()(A first, B second) { return 0.0; }
};
class VisitorAC
{
public:
float operator()(A first, B second) { return 1.0; }
float operator()(A first, C second) { return 2.0; }
float bar(A first, C second) { return 3.0; }
};
int main()
{
VisitorAB vab;
VisitorAC vac;
Visitor v;
A a;
B b;
C c;
v.foo = boost::bind2nd(vac, c); //Gives me a compile error
return 0;
}
I've also tried these:
v.foo = boost::bind(vac, _1, c);
v.foo = boost::bind<float (a)>(vac, _1, c);
v.foo = boost::bind( boost::mem_fun_ref(&VisitorAC::bar), c);
I finally found after about three hours that if I used v.foo =
boost::bind<float>(vac, _1, c) that I could cajole the program into
compiling.
I'm confused on the use of bind. What could I have done to make this
work with boost::bind2nd? Why is it that boost::bind only works when
you specify a template parameter?
Your answers are appreciated.