簡體   English   中英

Cython“模板參數中的未知類型”錯誤

[英]Cython “Unknown type in template argument” error

我正在嘗試為以下C ++類創建Python包裝器。

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

對於Python包裝器,將Numpy的ndarray作為參數並從中創建向量。 然后,它嘗試將地址傳遞給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
....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM