我知道<<运算符也可以作为方法函数重载。当我使用模板参数时,问题出现了。
我意识到我需要指定类,因此我写了< A>在运营商之前<< 。
我应该在哪里写< A>如果我想使用该对象< < ostream语法?
#include <iostream>
#include <malloc.h>
using namespace std;
class A
{
private:
int x;
public :
A(int i = 0)
{
x = i;
}
A operator + (A temp)
{
return x + temp.x;
}
template <class T>
ostream & operator << (ostream &);
};
template <class T>
ostream& A::operator<<(ostream &o)
{
o<<x;
return o;
}
int main()
{
A a(33),b(-21);
(a+b).operator<<<A>(cout);
//a+b<<cout; ????
}