wenbo228228 2015-11-13 07:47 采纳率: 58.3%
浏览 2009

模板类重载=操作符

类定义

 template <class T>
class Element   //数据表的元素
{
public:
    T key;
    //在此处添加除关键码之外的其他数据成员

    Element<T>& operator = (Element<T>& x)
    {
        key = x.key;
        return *this;
    }
    Element<T>& operator = (T& x)
    {
        key = x.key;
        return *this;
    }
    bool operator == (Element<T>& x){ return key == x.key; }
    bool operator <= (Element<T>& x){ return key <= x.key; }
    bool operator > (Element<T>& x){ return key > x.key; }
    bool operator < (Element<T>& x){ return key < x.key; }
    template <class T>
    friend ostream& operator << (ostream& out, Element<T>& x)
    {
        out << x.key;
        return out;
    }
};

调用,错误信息如注释所示

 Element<int> e;
    e = 3;  //  2   IntelliSense:  没有与这些操作数匹配的 "=" 运算符
    cout << e;
  • 写回答

2条回答 默认 最新

  • Robot-C 2015-11-13 07:56
    关注

    
    模板类中操作符重载问题(">"重载)

    在模板类中输入流“>>”和输出流“>"的重载。
    一、将输出流">"重载的实现写在类中

    #include "stdafx.h"
    #include   
    using   namespace   std;  
       
    templateclass T>  
    class Test  
    {  
       public:  
       ......
    答案就在这里:模板类中重载<<和>>操作符
    ----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?