用模板类提升python

Nib*_*ose 5 c++ boost boost-python

我创建了一个环形缓冲区,我想使用boost将该类导入Python.当我试图得到它的错误.

ring.cpp: In function ‘void init_module_ring()’:
ring.cpp:130:16: error: wrong number of template arguments (1, should be 4)
 class_<Ring>("Ring").Please help me. Thanks in Advance.
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

#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 i =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)
    ;
}
Run Code Online (Sandbox Code Playgroud)

Tam*_*lei 5

模板不是类。您需要实例化您的模板(即Ring<int>代替Ring)。

class_<Ring<int>>("IntRing", init<int>())
    .def("insert", &Ring<int>::insert)
    .def("display_all", &Ring<int>::displayall)
;
Run Code Online (Sandbox Code Playgroud)

另外,template <class T>原始代码中的部分:

class_<Ring>("Ring")
    template <class T> // error
    .def(init<int>())
    .def("insert", &Ring::insert)
    .def("display_all", &Ring::displayall)
;
Run Code Online (Sandbox Code Playgroud)

是语法错误。它表明您希望能够以通用方式绑定到模板,不幸的是这是不可能的。原因是模板在编译时被实例化,即编译器需要知道模板将被使用的确切类型。如果您与 python 接口,则无法提前知道,因为它是在运行时决定的。

在幕后,Boost.Python 生成包装函数,这些函数PyObject从 python中获取s 并将它们转换为参数的强类型值(并将返回值返回到PyObjects)。它只能这样做,因为它知道将动态值转换为/从的类型。

您能做的最好的事情是创建不是泛型的类,而是使用python 对象进行操作

编辑:为了回应您的评论(“错误:'init' 未在此范围内声明”),我认为问题在于您只包含一个 Boost.Python 标头。无论是#include <boost/python.hpp>或包括所有其他部分,你需要(一个是init.hpp)。