python - Cython "Unknown type in template argument"错误

标签 python c++ cython

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

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

对于 Python 包装器,获取 Numpy 的 ndarray 作为参数并从中创建一个 vector 。然后,它尝试将地址传递给 C++ 构造函数及其函数“func”。

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

我正确地导入了 vector 和 pair,但我不明白为什么 Cython 会提示这个。任何帮助将不胜感激!

最佳答案

您需要导入 vectorpair 的定义,以便 Cython 知道它们,即:

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

关于python - Cython "Unknown type in template argument"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50716886/

相关文章:

python - 为什么 DNS 响应不同?

c++ - 清除 Bison 生成的解析器的解析器状态

c++ - MSVS 2012 "ambiguous call to overloaded function"中的编译器错误

cython - 如何将指针传递给 Cython 中的 c 函数?

cython:如何声明一个没有返回值的 cdef 函数

python - 数据帧的排序子数组的滚动平均值

python - Spark 1.4 增加 maxResultSize 内存

python - 带有 HiddenInput 的 Django 的 ModelForm 返回无效

C++ 复古新手 : address references

python - Malloc 与 nogil 一起使用安全吗?