2

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++).

3 Answers 3

2

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*
2
  • 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
    May 9, 2010 at 9:25
  • 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
    May 11, 2010 at 13:10
2

Try with:

%typemap(in) (unsigned char *) = (char *);
-2

This can be solved by modifying the lines 100 to 110

self.source = uhd_receiver(options.args, symbol_rate,
                           options.samples_per_symbol,
                           options.rx_freq, 
                           options.rx_gain, options.spec, options.antenna,
                           options.verbose)

self.sink = uhd_transmitter(options.args, symbol_rate,
                            options.samples_per_symbol,
                            options.tx_freq, 
                            options.tx_gain, options.spec, options.antenna,
                            options.verbose)

to the following

self.source = uhd_receiver(options.args, symbol_rate,
                           options.samples_per_symbol, 
                           options.rx_freq, 
              ---------->  options.lo_offset,
                           options.rx_gain, options.spec, options.antenna,
              ---------->  options.clock_source,
                           options.verbose)

self.sink = uhd_transmitter(options.args, symbol_rate,
                            options.samples_per_symbol, 
                            options.tx_freq,
              ---------->   options.lo_offset, 
                            options.tx_gain, options.spec, options.antenna,
              ---------->   options.clock_source, 
                            options.verbose)

Best of luck

1
  • Welcome to Stackoverflow. Please add the reason to do so. Oct 23, 2015 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.