关于SWIG封装的重载C++构造函数传递参数数量/类型的问题

2 投票
3 回答
4354 浏览
提问于 2025-04-15 22:04

我正在尝试用swig把一个别人写的c++类(我们叫它“Spam”)包装起来,以便在Python中使用。解决了几个问题后,我终于可以在Python中导入这个模块了,但当我尝试创建这个类的对象时,出现了以下错误:

 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)

我在网上查了一下,发现这个错误可能是因为参数的类型不对,而不是数量不对(这点有点让人困惑),但我还是没法确定具体问题在哪。我怀疑问题出在把一个字符串作为第一个参数传递上,但我不知道该怎么解决这个问题(请记住,我几乎对c/c++一无所知)。

3 个回答

-2

这个问题可以通过修改第100到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)

修改成下面这样:

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)

祝你好运!

2

试试这个:

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

SWIG把字符串当作'char*'来处理。你用'unsigned char *'可能让它感到困惑。你可以选择把函数的参数改成'char *',或者提供一个类型映射(typemap):

%typemap(in) unsigned char * = char*

撰写回答