Cython"模板参数中的未知类型"错误

时间:2018-06-06 09:31:05

标签: python c++ cython

我正在尝试为C ++类创建Python包装器。

A(vector<pair<double, double>>* points, double r_cutoff) 
void func(vector<pair<double, double>>* offset)

Python包装器将Numpy的ndarray作为参数并从中创建一个向量。然后,它尝试将地址传递给C ++构造函数及其函数&#34; func&#34;。

cdef extern from "cell.h" namespace "cl":
    cdef cppclass A:
        A(vector[pair[double, double]]* points, double r_cutoff) except +
        void func(vector[pair[double, double]]* offset)

cdef class PyA:
    cdef A* thisptr
    def __cinit__(self, np.ndarray points, double r_cutoff):
        cdef vector[pair[double, double]] vec
        vec.resize(points.shape[0])
        for i in range(points.shape[0]):
            vec[i].first = points[i][0]
            vec[i].second = points[i][1]

        self.thisptr = new A(&vec, r_cutoff)

    def func(self, np.ndarray offset):
        cdef vector[pair[double, double]] vec
        vec.resize(offset.shape[0])

        for i in range(offset.shape[0]):
            vec[i].first = offset[i][0]
            vec[i].second = offset[i][1]
        self.thisptr.func(&vec)

但它抱怨

中有一种未知的类型
  def func(self, np.ndarray offset):
        cdef vector[pair[double, double]] vec
                        ^
------------------------------------------------------------

file.pyx:27:25: unknown type in template argument

我正确导入了矢量和对,但我不明白为什么Cython会抱怨这个。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

您需要隐藏vectorpair的定义,因此Cython知道它们,即:

from libcpp.vector cimport vector
from libcpp.utility cimport pair
....