指向类的指针的STL映射的SWIG typemap

2024-09-17 19:42:47 发布

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

我试图将int(C++)的STL映射(版本3)向类指针,到Python 3:

示例.h

#include <map>

using namespace std;

class Test{};

class Example{
public:
    map<int,Test*> my_map;
    Example() 
    {
        int a=0;
        Test *b = new Test();
        this->my_map[a] = b;
    }
};

示例。i

^{pr2}$

没有错误,但是现在在python3中,运行

import example
t = example.Example()
t.my_map

退货

<Swig Object of type 'map< int,Test * > *' at 0x10135e7b0>

而不是字典。它还有一个指向地图的指针,而不是一个地图。如何编写正确的%typemap将STL映射转换为python3字典?在

我已经能够为例如int到int的映射这样做-它是一个类的指针给我带来麻烦。在

谢谢。在


Tags: test版本示例map字典includeexamplemy
1条回答
网友
1楼 · 发布于 2024-09-17 19:42:47

让我从SWIG手册中为您提供相关条目。。。here

这告诉您成员变量my_map是通过SWIG生成的getter访问的,getter返回一个map<int,Test*> *(或者引用,如果您给出%naturalvar指令)。因此,必须编写out typemap来处理map<int,Test*> *,而不是map<int,Test*>。在

相关问题 更多 >