我遇到類似的問題Wrap std::vector of std::vectors, C++ SWIG Python - 但它不僅僅是簡單的C++解析。我在我的C++代碼SWIG包裝矢量向量(C++到Python) - 如何將內部向量識別爲代理對象?
namespace ns {
typedef unsigned long long uint64_t;
typedef std::vector<uint64_t> Vector;
typedef std::vector<Vector> VectorOfVectors;
class MyClass {
/// ...
/// Returns a reference to the internal vector allocated in C++ land
const VectorOfVectors &GetVectors() const;
};
}
而在夜風包裝
%module myswig
// ...
%template(Uint64V) std::vector<ns::uint64_t>;
%template(VUint64V) std::vector<std::vector<ns::uint64_t> >;
所以包裝工作正常,包括類以下的,我可以檢索向量的類的載體OK:
import myswig
m = myswig.MyClass()
v = m.GetVectors()
print v
這給了我:
<myswig.VUint64V; proxy of <Swig Object of type 'std::vector< std::vector< ns::uint64_t,std::allocator<ns::uint64_t> > > *' at 0x994a050> >
但是,如果我訪問向量中的元素,我不會得到一個代理是一個myswig.Uint64V - 這是我的問題。
x = v[0]
print x
我倒是希望得到的是:
<myswig.Uint64V; proxy of <Swig Object of type 'std::vector< ns::uint64_t, std::allocator<ns::uint64_t> > *' at 0x994a080> >
相反,我越來越:
(<Swig Object of type 'ns::uint64_t *' at 0x994a080>, <Swig Object of type 'ns::uint64_t *' at 0x994a098>)
也就是說,索引向量的向量是給我一個2條目元組,而不是我需要的向量類的代理(以便訪問內部向量與訪問其他向量一樣簡單)。
我也得到警告:
swig/python detected a memory leak of type 'ns::uint64_t *', no destructor found.
原因當然沒有這種類型定義的析構函數。
任何想法?
我有const引用和指針和const指針綁定的問題,你能提供更多的信息嗎? – squid 2016-01-08 02:52:28
定義線非常重要......但它同時是......不顯眼的。我想知道你是如何找到它的。 – squid 2016-01-14 04:39:01