When I said “outside” I meant standalone function.
Moreover I see you’ve added property with the same functions. This won’t work.
I would recommend you to look at docs
-http://www.boost.org/doc/libs/1_55_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.constructors
Valid wrap with untouched class declaration.
class Turtle
{
friend const PV& GetHeading(const Turtle& t);
friend const PV& GetLeft(const Turtle& t);
friend const P& GetPoint(const Turtle& t);
friend void SetPoint(Turtle& t, const P& p);
public:
...
private:
PV h;
PV l;
};
BOOST_PYTHON_MODULE(TurtleWrapper)
{
class_("Turtle")
;
def("GetHeading",&Turtle::GetHeading)
def("GetLeft",&Turtle::GetLeft)
}
Anyway I would recommend you not to use friend functions to get something from
private section of class. If you’ll make methods your code will be cleaner.
Working code:
#include
struct PV{};
struct P{};
class Turtle
{
public:
const PV& GetHeading()
{
return mHeading;
}
const PV& GetLeft()
{
return mLeft;
}
const P& GetPoint()
{
return mPoint;
}
void SetPoint(const P& p)
{
mPoint = p;
}
private:
PV mHeading, mLeft;
P mPoint;
};
using namespace boost::python;
BOOST_PYTHON_MODULE(TurtleWrapper)
{
class_("Turtle")
.add_property("Heading", make_function(&Turtle::GetHeading,
return_internal_reference<>()))
.add_property("Left",make_function(&Turtle::GetLeft,
return_internal_reference<>()))
.add_property("Point", make_function(&Turtle::GetPoint,
return_internal_reference<>()), &Turtle::SetPoint)
;
}
On 18 Jul 2014, at 15:37, Kv Gopalkrishnan wrote:
> Hi
>Thanks Michael For the answer however I am a bit confused about the
> solution you gave. Do you mean to remove the GetHeading function and paste it
> outside class Turtle ?
>
> Jim here is a concrete example.
>
>
> #include
> #include
> #include
> #include
>
> using namespace std;
> using namespace cxxadt;
>
>
> class Turtle{
> friend const PV& GetHeading(const Turtle& t);
> public:
>
> Turtle();
> private:
>
> PV h;
>
> };
>
> now corresponding to this the wrapper i wrote for this
> #include
> #include
> #include
> #include
> #include
> #include
> #include"Turtle.h"
> #include
>
> #include
> #include
>
>
> using namespace std;
>
> using namespace boost::python;
>
> using namespace cxxadt;
>
>
>
>
> BOOST_PYTHON_MODULE(TurtleWrapper)
>
> {
>
>
>
> class_("Turtle")
>
>.def("GetHeading",&GetHeading) ;
>
> }
>
>
> class_("PV",init());
>
>
> class_("P",init());
>
>
> }
>
>
>
>
>
>
>
>
>
> Kind Regards
> KV
>
>
>
>
>
>
>
>
>
>
>
>
>
> On Fri, Jul 18, 2014 at 1:50 PM, Jim Bosch wrote:
> On Jul 18, 2014 6:32 AM, "Kv Gopalkrishnan"
> wrote:
> >
> > Hi Jim
> > Thank you for the answer. When I tried this solution
> > i.e. .def("GetHeading",&GetHeading)
> > error: ‘GetHeading’ was not declared in this scope
> >
> >
> > I get the above error.
> > Stating the obvious the error says function was not described in the
> > scope. Seems a bit nasty business is it so that I would have to write a get
> > function in someway in to public domain to get the function wrapped in to
> > boost?
> >
>
> I'm pretty sure that should have worked. Could you post a more complete
> example? I suspect the problem is elsewhere.
>
> Jim
>
> >
> > On Thu, Jul 17, 2014 at 3:12 PM, Jim Bosch wrote:
> >>
> >> The problem here is actually a C++ issue, not a Boost.Python one. Friend
> >> functions aren't considered to be part of the scope of the class that
> >> they're friends with, so when referring to them, just use e.g.
> >> "&GetHeading", not "&Turtle::GetHeading".
> >>
> >> Jim
> >>
> >>
> >> On Wed, Jul 16, 2014 at 7:52 AM, Kv Gopalkrishnan
> >> wrote:
> >>>
> >>> I want to expose a C++ friend functions to python using Python boost.
> >>> class Turtle{
> >>>friend const PV& GetHeading(const Turtle& t);
> >>>friend const PV& GetLeft(const Turtle& t);
> >>>friend const P& GetPoint(const Turtle& t);
> >>>friend void SetPoint(Turtle& t, const P& p);
> >>>public:
> >>>
> >>>...
> >>>
> >>>private:
> >>>PV h;
> >>>PV l;
> >>>
> >>> };
> >>>
> >>>
> >>> Here I have wrapped the classes PV and P so no problem there. I tried to
> >>> wrap the friend functions like regular functions. Like
> >>>
> >>> BOOST_PYTHON_MODULE(TurtleWrapper)
> >>> {
> >>> class_("Turtle")
> >>>.def("GetHeading",&Turtle::GetH