SWIG:包装 std::map<key 时编译器错误,val *>
SWIG: Compiler error when wrapping std::map<key, val *>
我发现了这个老问题,对于值是指针而不是键的地图,我也有类似的问题。我收到此编译器错误:
error: no member named 'type_name' in 'swig::traits<C>'
当我编写自己的类型图或使用SWIG"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*>;
SWIG 可能对类指针感到困惑,因为它的包装器无论如何都使用指针。 无论如何,SWIG文档说(粗体我的(:
本节中的库模块提供对标准C++库部分(包括 STL(的访问。SWIG对STL的支持是一项持续的努力。对某些语言模块的支持非常全面,但一些较少使用的模块没有编写那么多的库代码。
如果您可以自由更改实现,我看到两种解决方法有效。 我使用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> >
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- 'val' Arduino 错误代码之前的预期'('
- 在模板元编程中使用"struct xxx<>::val"导致错误
- SWIG:包装 std::map<key 时编译器错误,val *>
- C++ 异常情况下的错误 C2228(".val"的左侧必须具有类/结构/联合)