类定义
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;
模板类中操作符重载问题(">"重载)
在模板类中输入流“>>”和输出流“>"的重载。
一、将输出流">"重载的实现写在类中
#include "stdafx.h"
#include
using namespace std;
templateclass T>
class Test
{
public:
......
答案就在这里:模板类中重载<<和>>操作符
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。
#include "stdafx.h"
#include <iostream>
template <class T>
class Element //数据表的元素
{
public:
T key;
//在此处添加除关键码之外的其他数据成员
inline Element<T>& operator = (Element<T>& x)
{
key = x.key;
return *this;
}
inline Element<T>& operator = (T& x)
{
key = x;
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; }
};
int _tmain(int argc, _TCHAR* argv[])
{
Element<int> e;
int a = 3;
e=a;
std::cout << e.key;
return 0;
}
这样就可以了