我没有获得部分模板特化。 我的类(class)看起来像这样:
template<typename tVector, int A>
class DaubechiesWavelet : public AbstractWavelet<tVector> { // line 14
public:
static inline const tVector waveletCoeff() {
tVector result( 2*A );
tVector sc = scalingCoeff();
for(int i = 0; i < 2*A; ++i) {
result(i) = pow(-1, i) * sc(2*A - 1 - i);
}
return result;
}
static inline const tVector& scalingCoeff();
};
template<typename tVector>
inline const tVector& DaubechiesWavelet<tVector, 1>::scalingCoeff() { // line 30
return tVector({ 1, 1 });
}
gcc 输出的错误是:
line 30: error: invalid use of incomplete type ‘class numerics::wavelets::DaubechiesWavelet<tVector, 1>’
line 14: error: declaration of ‘class numerics::wavelets::DaubechiesWavelet<tVector, 1>’
我尝试了多种解决方案,但都没有奏效。 有人给我提示吗?
最佳答案
template<typename tVector>
inline const tVector& DaubechiesWavelet<tVector, 1>::scalingCoeff() { // line 30
return tVector({ 1, 1 });
}
这是一个偏特化成员的定义,定义如下
template<typename tVector>
class DaubechiesWavelet<tVector, 1> {
/* ... */
const tVector& scalingCoeff();
/* ... */
};
它不是主模板“DaubechiesWavelet”的成员“scalingCoeff”的特化。需要这样的特化来传递所有参数的值,而您的特化不需要这样做。做你想做的事,你可以使用重载
template<typename tVector, int A>
class DaubechiesWavelet : public AbstractWavelet<tVector> { // line 14
template<typename T, int I> struct Params { };
public:
static inline const tVector waveletCoeff() {
tVector result( 2*A );
tVector sc = scalingCoeff();
for(int i = 0; i < 2*A; ++i) {
result(i) = pow(-1, i) * sc(2*A - 1 - i);
}
return result;
}
static inline const tVector& scalingCoeff() {
return scalingCoeffImpl(Params<tVector, A>());
}
private:
template<typename tVector1, int A1>
static inline const tVector& scalingCoeffImpl(Params<tVector1, A1>) {
/* generic impl ... */
}
template<typename tVector1>
static inline const tVector& scalingCoeffImpl(Params<tVector1, 1>) {
return tVector({ 1, 1 });
}
};
请注意,您使用的初始化语法仅适用于 C++0x。
关于C++:部分模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3216199/