2010-07-09 107 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

我沒有看到專門的課程。你必須專門研究這個課程,並且在課堂上專門研究這個方法。