尝试为要使用的库生成SWIG接口时出现错误。该代码包含一个从模板化类继承的类,该类包括默认值。但是,模板化类还具有不包含默认值的前向声明。我相信这令人困惑。
这是一个简单的例子:
frac.h
(父类):
#pragma once
// forward declaration
template <typename A, typename B>
class Frac;
// ... code using the forward declaraton
// definition
template <typename A=int, typename B=int>
class Frac
{
public:
A a;
B b;
double divide()
{
return a / b;
};
};
timestwo.h
(子类):
#pragma once
#include "frac.h"
class TimesTwo : public Frac<double>
{
public:
double getValue()
{
a = 10.5;
b = 4;
return divide() * 2;
}
};
mylib.i
文件:
%module mylib
%{
#include "timestwo.h"
%}
%include "frac.h"
/*
If no %template is used:
mylib.h:15: Warning 401: Nothing known about base class 'Frac< double >'. Ignored.
mylib.h:15: Warning 401: Maybe you forgot to instantiate 'Frac< double >' using %template.
*/
/*
If put here: %template(frac_d) Frac <double>;
mylib.i:15: Error: Not enough template parameters specified. 2 required.
*/
/*
If put here: %template(frac_d) Frac <double, int>;
timestwo.h:5: Warning 401: Nothing known about base class 'Frac< double >'. Ignored.
timestwo.h:5: Warning 401: Maybe you forgot to instantiate 'Frac< double >' using %template.
*/
%include "timestwo.h"
如mylib.i
的注释所示,由于我需要使用一个模板参数,因此我似乎无法正确实例化该模板,但是由于前向声明未指定默认值,因此它表示期待两个。
答案 0 :(得分:0)
这只是一个警告。您要实例化cn = {
"first_name": "supercool",
"email": "wow@example.com",
"username": "foo"
}
r = requests.post(urlEndpoint, data = cn)
还是致电Frac
?否则,它将起作用:
divide
如果您希望能够呼叫>>> import mylib
>>> t = mylib.TimesTwo()
>>> t.getValue()
5.25
,SWIG似乎不了解模板默认值。它可以通过用divide()
更新timestwo.h
来起作用,但是如果您不想修改标题,则可以通过以下更正来手动复制Frac<double,int>
文件中的定义:
.i
演示:
%module mylib
%{
#include "timestwo.h"
%}
%include "frac.h"
%template(frac_d) Frac<double,int>; // Frac<double> doesn't work as of SWIG 3.0.12.
// Declare the interface the way SWIG likes it.
class TimesTwo : public Frac<double,int>
{
public:
double getValue();
};