c++ - boost python with template class -


i created ring buffer , want import class python using boost. when trying getting error.

ring.cpp: in function ‘void init_module_ring()’: ring.cpp:130:16: error: wrong number of template arguments (1, should 4)  class_<ring>("ring").please me. in advance. 

here code:

#include <boost/python/def.hpp> #include<iostream> using namespace std; using namespace boost::python;   template <class t> class ring    {    public:    class iterator;    public:     unsigned int m_size;     unsigned int pos;     t *val;     ring():     m_size(0),pos(0),val(null){};     ring(int size):m_size(size),pos(0){         val=new t[m_size];     };     ring(const ring &other)         {         this->m_size = other.m_size;         this->pos= other.pos;         this->val = other.val;          }     ~ring()         {         delete[] val;         }     void insert(t data)         {         val[pos]= data;         pos++;         if(pos==m_size)         pos=0;          }     void displayall()     {        for(int =0;i<m_size;i++)        {        cout<<val[i]<<' ';        }     }     iterator begin()     {        return iterator(val);     }     iterator end()     {        return iterator(val + m_size);     }     unsigned int size()     {        return m_size;    }    void check()    {       cout<<val<<' ';       cout<<val+5;    }     }; template<class t> class ring<t>::iterator {    t *it_value;    public:    iterator():it_value(null){};    iterator(t *value)    {    it_value = value;    };    iterator(const iterator &other)    {        this->it_value = other.it_value;    }    friend ostream& operator<<(ostream &out,iterator it)       {       out<<*it.it_value;       return out;       }    iterator operator++()       {       it_value  +=1;       return (iterator(it_value));       }    iterator operator++(const int x)       {       it_value = it_value+ x+1;       return(iterator(it_value));       }    bool operator==(const iterator &other) const        {        return (*it_value == *(other.it_value));        };    bool operator!=(const iterator &other) const    {    return (!(*this == other));    };    iterator operator*()       {       return *this;       }    void display()        {        cout<<*it_value<<' ';        } };  boost_python_module(ring) {     class_<ring>("ring")         template <class t>         .def(init<int>())         .def("insert", &ring::insert)         .def("display_all", &ring::displayall)     ; } 

a template not class. need instantiate template (i.e. ring<int> instead of ring).

class_<ring<int>>("intring", init<int>())     .def("insert", &ring<int>::insert)     .def("display_all", &ring<int>::displayall) ; 

also, template <class t> part in original code:

class_<ring>("ring")     template <class t> // error     .def(init<int>())     .def("insert", &ring::insert)     .def("display_all", &ring::displayall) ; 

is syntax error. suggests expect able bind template in generic manner, unfortunately not possible. reason templates instantiated @ compile-time, i.e. compiler needs know exact types template going used with. if interfacing python, can't know in advance, because decided @ runtime.

under hood, boost.python generates wrapper functions take pyobjects python , convert them typed values parameters (and return values pyobjects). can because knows types convert dynamic values to/from.

the best can creating class not generic instead operates python objects.

edit: in response comment ("error: ‘init’ not declared in scope"), think problem including 1 boost.python header. either #include <boost/python.hpp> or include other parts need (one init.hpp).


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -