0

我想将 C++ 对象存储在 libcpp.map 中,但我无法让它工作。如果我在声明文件中声明地图,它甚至不适用于简单的整数。

.pxd file:

from libcpp.map cimport map
cdef class MyClass:
    cdef map[int,int] store


.pyx file:

cdef class MyClass:
    def __cinit__(self):
        self.store = map[int,int]()

以下错误:

cdef map[int,int] store
          ^
C++ classes not allowed as members of an extension type,
use a pointer or reference instead

为什么这不起作用?如果我在函数中声明它,它工作正常。

4

1 回答 1

2

从错误开始,您似乎需要存储一个指向它的指针并在堆上调用一个新版本,所以

cdef map[int,int] *store

self.store = new map[int,int]()
于 2012-06-03T13:23:17.473 回答