首页 > 其他 > 详细

[基础] 模板+友元类外定义

时间:2016-03-11 01:15:18      阅读:216      评论:0      收藏:0      [点我收藏+]

下面这种定义方式会报错: Undefined symbols for architecture x86_64

template <typename T>
class longint{
public:
    T num;
    longint (T a = 0) {
        num = a;
    }
    
    friend ostream& operator<< (ostream& out, const longint<T>& Lint);
    ~longint (){
        ;
    }
private:
};

template <typename T>
ostream& operator<< (ostream &out, const longint<T> & Lint) {
    return out<<Lint.num;
}

改法一:将重载运算符的T改成S就行了

//类内声明时
template <typename S>
friend ostream& operator<< (ostream& out, const longint<S>& Lint);

//类外定义时
template <typename S>
ostream& operator<< (ostream &out, const longint<S> & Lint) {
    return out<<Lint.num;
}

改法二:直接在类内定义

friend ostream& operator<< (ostream& out, const longint<T>& Lint) {
    return out<<Lint.num;
}

 

[基础] 模板+友元类外定义

原文:http://www.cnblogs.com/littletail/p/5264122.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!