3

我没有获得部分模板专业化。我的课看起来像这样:

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

我尝试了几种解决方案,但都没有奏效。有人对我有提示吗?

4

2 回答 2

3

我没有看到专门的课程。您必须专门化该类,并在其中专门化方法。

于 2010-07-09T20:15:25.923 回答
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。

于 2010-07-09T20:18:59.040 回答