【问题标题】:Partial template specialization of member function: "prototype does not match"成员函数的部分模板特化:“原型不匹配”
【发布时间】:2012-11-08 19:19:20
【问题描述】:

我正在尝试部分专门化非模板类的模板化成员函数:

#include <iostream>

template<class T>
class Foo {};

struct Bar {

  template<class T>
  int fct(T);

};

template<class FloatT>
int Bar::fct(Foo<FloatT>) {}


int main() {
  Bar bar;
  Foo<float> arg;
  std::cout << bar.fct(arg);
}

我收到以下错误:

c.cc:14: error: prototype for ‘int Bar::fct(Foo<FloatT>)’ does not match any in class ‘Bar’
c.cc:9: error: candidate is: template<class T> int Bar::fct(T)

如何修复编译器错误?

【问题讨论】:

    标签: c++ templates template-specialization partial-specialization


    【解决方案1】:

    不允许对函数(成员或其他)进行部分特化。

    使用重载:

    struct Bar {
    
      template<class T>
      int fct(T data);
    
      template<class T>    //this is overload, not [partial] specialization
      int fct(Foo<T> data);
    
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-15
      • 2012-04-11
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多