2010-07-09 104 views
3

我没有得到部分模板专业化。 我的类看起来是这样的:C++:部分模板专业化

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>’ 

我试过几种解决方案,但没有奏效。 有人对我有暗示吗?

+0

'result(i)'?这不应该是'结果[我]'而是? – 6502 2010-07-09 20:21:29

+0

我使用boost的ublas,所以你可以使用()运算符 – Manuel 2010-07-09 20:39:56

回答

3
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中工作。

+0

干杯! PS:我知道初始化问题,谢谢。 – Manuel 2010-07-09 20:39:16

4

我没有看到专门的课程。你必须专门研究这个课程,并且在课堂上专门研究这个方法。