我发现,对于值是指针而不是键的映射,这个老问题和我有类似的问题。我得到了这个编译器错误:
error: no member named 'type_name' in 'swig::traits<C>'无论是在我自己编写类型地图时还是在我使用"std_map.i“类型地图时,都会发生这种情况。我需要采取哪些额外步骤来为指向类型提供一个type_name?
最小工作实例:
%module stdmap;
%include "std_map.i"
%{
class C
{
public:
C() {};
};
%}
class C
{
public:
C();
};
%template(mymap) std::map<int, C*>;发布于 2018-02-04 20:20:14
SWIG可能对类指针感到困惑,因为它的包装器无论如何都使用指针。在任何情况下,SWIG文档都说(粗体地雷):
本节中的库模块提供了对标准C++库(包括STL )部分的访问。对STL的SWIG支持是正在进行的一项工作。对某些语言模块的支持是相当全面的,但一些较少使用的模块没有编写那么多库代码。
如果您可以自由地更改实现,那么我看到了两个可行的解决方案。我使用Python作为测试的目标语言:
std::map<int,C>
%module stdmap
%include "std_map.i"
%inline %{
#include <memory>
class C
{
public:
C() {};
};
%}
%template(mymap) std::map<int, C>;输出(注意c是C*的代理对象):
>>> import stdmap
>>> c = stdmap.C()
>>> m = stdmap.mymap()
>>> m[1] = c
>>> c
<stdmap.C; proxy of <Swig Object of type 'C *' at 0x00000263B8DA5780> >std::map<int, std::shared_ptr<C>>
%module stdmap
%include "std_map.i"
%include "std_shared_ptr.i"
%shared_ptr(C)
%inline %{
#include <memory>
class C
{
public:
C() {};
};
%}
%template(mymap) std::map<int, std::shared_ptr<C> >;输出(c现在是shared_ptr代理):
>>> import stdmap
>>> c = stdmap.C()
>>> m = stdmap.mymap()
>>> m[1] = c
>>> c
<stdmap.C; proxy of <Swig Object of type 'std::shared_ptr< C > *' at 0x00000209C44D5060> >https://stackoverflow.com/questions/48579695
复制相似问题