[C++-sig] Returning std::vector<int*> to python
Pierre Barbier de Reuille
pierre.barbier at cirad.fr
Wed Sep 21 10:15:08 CEST 2005
You should have a look there :
http://www.boost.org/libs/python/doc/v2/indexing.html
Otherwise, just export the instance of the class template as any other
class.
Pierre
Linh.H.Phan at jpl.nasa.gov a écrit :
> 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