码迷,mamicode.com
首页 > 其他好文 > 详细

类模板成员函数默认值问题:an out-of-line definition of a member of a class template cannot have default arguments

时间:2018-05-29 21:13:10      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:成员   定义   模板类   col   bsp   pen   问题:   类模板成员   编译器   

template <typename T>
class A {
    void fun(int a = 0);
};

template<typename T>
void A<T>::fun(int a = 0) {
    /*
    */
}

对于类似上文代码,VS编译器会报 “an out-of-line definition of a member of a class template cannot have default arguments”错误。

其原因在于:带有默认参数值的模板类成员不能在类外进行定义,修改方式有两种。

第一种在类内进行定义:

template <typename T>
class A {
    void fun(int a = 0){
       /*
      */
    }
};

第二种在类外进行定义时,将默认值去掉:

template <typename T>
class A {
    void fun(int a = 0);
};

template<typename T>
void A<T>::fun(int a /*= 0*/) {
    /*
    */
}

 

类模板成员函数默认值问题:an out-of-line definition of a member of a class template cannot have default arguments

标签:成员   定义   模板类   col   bsp   pen   问题:   类模板成员   编译器   

原文地址:https://www.cnblogs.com/code-wangjun/p/9107867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!