【发布时间】:2018-10-26 13:30:28
【问题描述】:
我在尝试为我想使用的库生成 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 的 cmets 所示,我似乎无法正确实例化模板,因为我需要使用一个模板参数,但由于前向声明没有指定默认值,它说它期待两个.
【问题讨论】: