C++:部分模板特化

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

我尝试了几种解决方案,但都没有起作用。有人能给我一点提示吗?

result(i)?难道不应该是result[i]吗? - 6502
我正在使用boost的ublas,因此您可以使用()运算符。 - Manuel
2个回答

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。


3

我看不到这个类有专门化。你必须对这个类进行专门化,并在其中定义方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接