加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 成员函数的部分模板特化:“原型不匹配”

发布时间:2020-12-16 03:46:33 所属栏目:百科 来源:网络整理
导读:我试图部分特化一个未模板类的模板化成员函数: #include iostreamtemplateclass Tclass Foo {};struct Bar { templateclass T int fct(T);};templateclass FloatTint Bar::fct(FooFloatT) {}int main() { Bar bar; Foofloat arg; std::cout bar.fct(arg);}
我试图部分特化一个未模板类的模板化成员函数:
#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)

如何修复编译器错误?

解决方法

不允许部分特殊化功能(成员或其他).

使用过载:

struct Bar {

  template<class T>
  int fct(T data);

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

};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读