Cython C++和STD::地图处理

2024-04-23 06:20:39 发布

您现在位置:Python中文网/ 问答频道 /正文

<>我试图用Cython接口C++类,但是作为一个参数STD::你知道吗

你知道吗示例.pxd文件:

from libcpp cimport bool
from libcpp cimport string
from libcpp cimport map
from libcpp.utility cimport pair


cdef extern from "../SampleProg.h":
        cdef extern from "<map>" namespace "std":
            cdef cppclass SampleProg:
            SampleProg() except +

            void setInitialTemplateParameters(map[string,XdmValue*] parameters, bool tunnel)

            # Get all parameters as a std::map
            map[string,XdmValue*]& getParameters()

            # Get all properties as a std::map
            map[string,string]& getProperties()

你知道吗示例程序.pyx文件:

cimport sampleProg
from libcpp cimport bool
from libcpp cimport map
from libcpp.utility cimport pair
from libcpp cimport string

cdef class PySampleProg:

     def set_initial_template_parameters(self, bool tunnel, **kwds):

        cdef map[str, PyXdmValue ] parameters
        cdef bool c_tunnel
        c_tunnel = tunnel
        cdef PyXdmValue value_
        for key, value in kwds.items():
                if isinstance(value, PyXdmValue):
                        value_ = value
                        parameters[key] = value_.thisvptr

        if len(kwds) > 0:
            self.setInitialTemplateParameters(parameters, c_tunnel);
我似乎找不到构造Cython中的C++的例子。我在pxd和pyx文件中都遇到了这个错误:

void setInitialTemplateParameters(map[string,XdmValue*] parameters, bool tunnel)

^

sampleProg.pxd:186:46: 'map' is not a type identifier


Tags: 文件frommapstringvaluetunnelboolparameters
1条回答
网友
1楼 · 发布于 2024-04-23 06:20:39

这看起来有点乱,可能离工作还有一点距离。但是,您的“map is not a type identifier”错误很容易解释。你想要:

from libcpp.map cimport map

(即{{CD1}}只获得^ { CD2>}文件,而不是定义在其中的^ {CD3>} C++类)。这同样适用于string。你知道吗

相关问题 更多 >