tags:

views:

34

answers:

1

I am trying to wrap a c++ class (let's call it "Spam") written by someone else with swig to expose it to Python. After solving several problems, I am able to import the module in python, but when I try to create an object of such class I obtain the following error:

 foo = Spam.Spam('abc',3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "Spam.py", line 96, in __init__
    this = _Spam.new_Spam(*args)
NotImplementedError: Wrong number of arguments for overloaded function 'new_Spam'.
  Possible C/C++ prototypes are:
    Spam(unsigned char *,unsigned long,bool,unsigned int,SSTree::io_action,char const *)
    Spam(unsigned char *,unsigned long,bool,unsigned int,SSTree::io_action)
    Spam(unsigned char *,unsigned long,bool,unsigned int)
    Spam(unsigned char *,unsigned long,bool)
    Spam(unsigned char *,unsigned long)

Googling around, I realized that the error is probably caused by the type of the arguments and not by the number (which is quite confusing), but I still cannot identify. I suspect the problem lies in passing a string as the first argument, but have no idea on how to fix it (keep in mind that I know almost no c/c++).

A: 

SWIG treats strings as 'char*'. Your use of 'unsigned char *' is most likely confusing it. You can either change the signature to 'char *' or provide a typemap:

%typemap(in) unsigned char * = char*
Aaron Saarela
Yes, through trial and error I figured out that the problem was there. I was thinking of writing a subclass of the original problematic class with a constructor casting the parameter, since I hadn't figured out how to use typemaps. I'll try your solution and let you know...
MiKo
No luck: I tried adding the typemap to my interface file, but I get a sintax error. I guess I should study a bit more typemaps.
MiKo