[C++-sig] Returning std::vector<int*> to python

Linh.H.Phan@jpl.nasa.gov phan at grover.jpl.nasa.gov
Tue Sep 20 20:55:54 CEST 2005


Hi Pierre,

Is there a simple tutorial somewhere that shows this process?

Thank you,

Linh
>Hi,
>
>If you want to return a value, you need to export it (or be sure it is
>exported by Boost.Python), so here you need to define your Python class
>for intVec.
>
>Then, you have to know that reference or pointer on Integer cannot
>exists "as is" in Python. You'll have to wrap them ... that may be a
>problem to export your intVec class.
>
>Pierre
>
>
> Linh.H.Phan at jpl.nasa.gov a e'crit :
> > Hi,
> > 
> >   I'm having a problem returning a std::vector<int*> to python:
> > 
> > #include <vector>
> > typedef std::vector<int*> intVec;
> > class Node
> > {
> > public:
> >   Node() {}
> >   intVec& getIntVec() {
> >     return intvec;
> >   }
> >   int computeSum(const intVec& intvec) {return 1;}
> >   intVec intvec;
> > };
> > 
> > #include <boost/python/module.hpp>
> > #include <boost/python/def.hpp>
> > #include <boost/python.hpp>
> > using namespace boost::python;
> > 
> > BOOST_PYTHON_MODULE(hello)
> > {
> >     class_<Node>("Node", init<>())
> >       .add_property("intvec"
> >         , make_getter(&Node::intvec, return_internal_reference<>()))
> >       .def("getIntVec", (intVec&(Node::*)())&Node::getIntVec,
> > 	   return_internal_reference<>())
> >       .def("computeSum", &Node::computeSum)
> >     ;
> > }
> > 
>>> 
>>>>>>import hello
>>>>>>test = hello.Node()
> >>>>test.computeSum(test.intvec)
> > 
> > TypeError: No Python class registered for C++ class std::vector<int*, std::allocator<int*> >
>>> 
> >>>>test.computeSum(test.getIntVec())
> > 
> > TypeError: No Python class registered for C++ class std::vector<int*, std::allocator<int*> >
> > 
> > 
> > Can someone please help?
> > 
> > Thank you very much!
> > 
> > Linh
> > _______________________________________________
> > C++-sig mailing list
> > C++-sig at python.org
> > http://mail.python.org/mailman/listinfo/c++-sig
> 

-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68



More information about the Cplusplus-sig mailing list