重载输出流运算符<<时,为什么只能输出单个变量的值,而不能输出表达式的值?
原理是什么?
#include <bits/stdc++.h>
using namespace std;
#define N 1000
struct Point
{
int x, y;
Point(){}
Point(int a, int b):x(a),y(b){}
Point operator + (Point &b)
{
return Point(x+b.x, y+b.y);
}
friend ostream& operator << (ostream& os, Point &p)
{
os<<"("<<p.x<<','<<p.y<<")";
return os;
}
};
int main()
{
Point p(1,2), q(3,4), r;
cout << p << ' ' << q << endl;
r = p + q;
cout << r << endl;
//以上编译都过了 能正常运行
cout << (p+q); //这行写编译不过, 请问为什么?
//报错:[Error] cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
return 0;
}