本帖最后由 qq1242009750 于 2018-9-10 11:52 编辑
Node operator*(double n,const Node& p){
return p*n; }
参数太多了,这个最多也就跟一个参数 自己 + 1参数 == 二元运算符
#include <iostream>
#define rep(i,a,n) for(int i=a;i<n;++i)
#define pb push_back
#define close() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int, int>P;
class Node {
private:
double x, y;
public:
Node(int i) { x = i; y = i; }
Node() { x = 0, y = 0; }
Node(double x, double y) { this->x = x, this->y = y; }
Node operator+(const Node &p) {
Node sum;
sum.x = x + p.x;
sum.y = y + p.y;
return sum;
}
Node operator-(const Node &p) {
Node sum;
sum.x = x - p.x;
sum.y = y - p.y;
return sum;
}
void Show() {
cout << x << " " << y << endl;
}
Node operator*(double n) {
Node sum;
sum.x = x * n;
sum.y = y * n;
return sum;
}
// friend Node operator*(double n,Node p){
// Node sum;
// sum.x=p.x*n;
// sum.y=p.y*n;
// return sum;
// }
Node operator*(Node &n) {
Node tmp;
tmp.x = x * n.x;
tmp.y = y * n.y;
return tmp;
}
};
int main()
{
Node p1;
Node p2(3, 4);
Node p3(4, 5);
p2 = p2 + p3;
p2 = (Node)5 * p2; //把5强制转换为Node类型 但前提是Node必须有一个int类型的构造函数 不用此方法请使用友元函数
p2.Show();
return 0;
}
|