6

这是我的 cython 程序

cdef struct Node:
    int v
    Node* next
    Node* pre

def f(int N):
    cdef:
        vector[Node*] narray
        int i
    narray.assign(N, 0)
    for i in xrange(N):
        narray[i] = 0

Cython 编译结果:

Error compiling Cython file:
------------------------------------------------------------
...
    cdef:
        vector[Node*] narray
        int i
    narray.assign(N, 0)
    for i in xrange(N):
        narray[i] = 0
             ^
------------------------------------------------------------

testLinkList.pyx:107:14: Compiler crash in AnalyseExpressionsTransform

但我可以使用push_back()在向量末尾附加值或使用int而不是Node*. 怎么了?

4

1 回答 1

2

你使用的是什么版本的 Cython?0.20.1 版适用于我的代码:

# distutils: language=c++

from libcpp.vector cimport vector

cdef struct Node:
    int v
    Node* next
    Node* pre

def f(int N):
    cdef:
        vector[Node*] narray
        int i
    narray.assign(N, NULL)
    for i in range(N):
        narray[i] = NULL

并使用此setup.py文件:

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules=cythonize("test_vector.pyx"))
于 2014-05-15T14:54:11.737 回答