【问题标题】:boost python with template class使用模板类提升 python
【发布时间】:2016-05-09 09:17:09
【问题描述】:

我创建了一个环形缓冲区,我想使用 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.

这是我的代码:

#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)
    ;
}

【问题讨论】:

    标签: c++ boost boost-python


    【解决方案1】:

    模板不是类。您需要实例化您的模板(即Ring&lt;int&gt; 而不是Ring)。

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

    另外,原始代码中的template &lt;class T&gt; 部分:

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

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

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

    您能做的最好的事情是创建非通用类,而是使用python objects 操作。

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

    【讨论】:

    • 第一个也不起作用,它给我错误“错误:'init'未在此范围内声明”。
    • 是的,我检查并删除了这个 'using namespace boost::python' 。但仍然出现错误。
    • 我包含 init.hpp 标头,但仍然得到相同的错误 -'error: provided for 'template class boost::python::class_ ' 模板 class class_; '
    • 错误:模板参数的数量错误(1,应该是 4)class_ >("IntRing")
    • 现在收到此错误:local/include/boost/python/class.hpp:265:11:注意:模板参数扣除/替换失败:ring.cpp:134:21:注意:无法转换'Ring()'(输入 'Ring')到输入 'const char*' .def(Ring()) .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 2022-12-08
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 2021-01-21
    相关资源
    最近更新 更多