<分区>
我想弄清楚为什么我不能像调用 operator+ 作为成员函数那样调用重载运算符 << 作为成员函数。
#include "pch.h"
#include <iostream>
using namespace std;
class A
{
int x;
public: A(int i = 0) { x = i; }
A operator+(const A& a) { return x + a.x; }
template <class T> ostream& operator<<(ostream&);
};
template <class T>
ostream& A::operator<<(ostream& o) { o << x; return o; }
int main()
{
A a1(33), a2(-21);
a1.operator+(a2);
a1.operator<<(cout); // Error here
return 0;
}