【发布时间】:2017-03-06 11:41:05
【问题描述】:
我想重载代码块中的插入和提取运算符。
这是我的代码:
#include<iostream>
#include<conio.h>
enter code here
using namespace std;
class point
{
private:
int x,y;
public:
point(int a, int b){
a=x;
b=y;
}
friend istream &operator >>(istream &i, point &p);
friend ostream &operator <<(ostream &o, point &p);
};
istream &operator>>(istream &i, point &p)
{
cout << "Enter the coordinates of x and y";
i >> x >> y;
return i;
}
ostream &operator<<(ostream &o, point &p)
{
cout << "coordinates are :";
o << x << endl;
o << y;
return o;
}
void main()
{
point p1(3,4);
cin >> p1;
cout << p1;
}
我收到以下错误:
“x”未在此范围内清除
“y”未在此范围内清除
请帮助解决这个问题。
【问题讨论】:
-
函数中的point() x 变量未知。
-
i >> x >> y;->i >> p.x >> p.y; -
也在你的构造函数中:` a=x; b=y;` 你的意思可能正好相反:
x=a; y=b;,但最好还是使用point(int a, int b) : x(a), y(b) { } -
如果你的问题得到解决,关闭一个问题总是一个好主意:)
标签: c++ class operator-overloading