Thanks Stefan!

I solved it by add below typemap:
%typemap(out) MyObject* {
  $result = SWIG_NewPointerObj($1, SWIGTYPE_p_MyObject, 0);

But I still do not understand why it works for KEY of map without this typemap but the VALUE does not.

Thanks for your patient guide.

Zhenguo Wang

On Tue, Jun 28, 2011 at 1:25 AM, Stefan Zager <> wrote:
I might be able to help if you can provide some sample code.


On Mon, Jun 27, 2011 at 1:24 AM, Zhenguo Wang <> wrote:

Thanks Stefan.


Your suggestion works – the returned vector can be iterated.


But new issue comes – once we want to print the object in the map, the key is correct (MyObject*), but the value is not, they are MyBase* now, but we expected it as MyObject* also.


  1. MyObject is inherit from MyBase.
  2. The C++ function does assemble the correct data in the return vector and map – all is MyObject*.


I used one full day on this, but still can’t get the reason.


Could you please give some suggestions?



Zhenguo Wang

On Thu, Jun 23, 2011 at 10:54 PM, Stefan Zager <> wrote:
You need to use a %template directive in your swig interface file; something like this:

%template(MyObjectMap) std::map<MyObject*, MyObject*>;
%template(MyObjectMapVector) std::vector< std::map<MyObject*, MyObject*> >;

Take a look at the SWIG docs for more info:


On Thu, Jun 23, 2011 at 1:02 AM, Zhenguo Wang <> wrote:
Hello all,

We are wrapping C++ into Python via Swig, one of our function is declare as below:
std::vector<std::map<MyObject*, MyObject*>*>& Search()

In Python, the function is called correctly, but we can't print out returned value.

1. To print the first element:
>>> result[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'SwigPyObject' object is unsubscriptable

2. To print the object directly:
>>> print result
<Swig Object of type 'std::vector< std::map< MyObject *,MyObject *,std::less< MyObject * >,s
td::allocator< std::pair< MyObject *const,MyObject * > > > *,std::allocator< std::map<
MyObject *,MyObject *,std::less< MyObject * >,std::allocator< std::pair< MyObject *const,MyObject * >
 > > * > > *' at 0x00BEE298>

In the wrapper code, the returned value been wrappered by SWIG_NewPointerObj() with the type swig_types[124].

Does anyone have experience on such kind of issue?

It's appreciative if any suggestion!

We are using Swig 2.0 and Python 2.5.

Thanks in advance!

Zhenguo Wang

Simplify data backup and recovery for your virtual environment with vRanger.
Installation's a snap, and flexible recovery options mean your data is safe,
secure and there when you need it. Data protection magic?
Nope - It's vRanger. Get your free trial download today.
Swig-user mailing list