# 关于 istream &operator >> ？

nanyou1 2008-03-05 07:12:30
istream &operator >> (istream &in,complex &s)
{
cout << "请输入实部的值： " ;
cin >> s.real ;
cout << "请输入虚部的值： " ;
cin >> s.image;
return in;
}
//定义的复数类的输入运算的重载

void main()
{
complex complex1;
cin >> complex1;
}

...全文
1047 8 打赏 收藏 举报

8 条回复

cxxer 2008-10-10
• 打赏
• 举报

``````
#include <iostream>
#include [itex]

using namespace std;

class myComplex
{
public:
myComplex(double r = 0.0, double i =0.0) {real = r; image = i;}
friend istream& operator>>(istream &, myComplex&);
friend ostream& operator<<(ostream &, myComplex&);

private:
double real, image;[code=C/C++]

};

istream& operator>>(istream &in, myComplex& c)
{
cout < < "请输入实部";
in >> c.real;
cout < < "请输入虚部";
in >> c.image;
return in;
}

ostream& operator < <(ostream &out, myComplex& c);
{
out < < c.real < < c.image > 0 ? '+':'-'  < < abs(c.image) < < 'i';
return out;
}

int main()
{
myComplex myCl;
cin >> mycl;
cout < < mycl < < endl;
return 0;
}

``````
cxxer 2008-10-10
• 打赏
• 举报

``````
#include <iostream>
#include [itex]

using namespace std;

class myComplex
{
public:
myComplex(double r = 0.0, double i =0.0) {real = r; image = i;}
friend istream& operator>>(istream &, myComplex&);
friend ostream& operator<<(ostream &, myComplex&);

private:
double real, image;[code=C/C++]``````

};

istream& operator>>(istream &in, myComplex& c)
{
cout << "请输入实部";
in >> c.real;
cout << "请输入虚部";
in >> c.image;
return in;
}

ostream& operator<<(ostream &out, myComplex& c);
{
out << c.real << c.image > 0 ? '+':'-' << abs(c.image) << 'i';
return out;
}

int main()
{
myComplex myCl;
cin >> mycl;
cout << mycl << endl;
return 0;
}
[/code]
study_live 2008-10-04
• 打赏
• 举报

``````
#include "stdafx.h"
#include <iostream>
using namespace std;

class complex
{
public:
int real;
int image;
};

istream &operator >> (istream &in,complex &s)
{
cout << "请输入实部的值： " ;
cin >> s.real ;
cout << "请输入虚部的值： " ;
cin >> s.image;
return in;
}
//定义的复数类的输入运算的重载

void main()
{
complex complex1;
cin >> complex1;

system("pause");
}
``````

VS编译也没错啊

• 打赏
• 举报

1 #include "iostream.h"
2 #include "iostream"
using namespace std;

• 打赏
• 举报

[Quote=引用 3 楼 jieao111 的回复:]

C/C++ code
istream &operator > > (istream &in,complex &s)
{
cout < < "请输入实部的值： " ;
in > > s.real ; //cin应该改成in
cout < < "请输入虚部的值： " ;
in > > s.image;
return in;
}
//定义的复数类的输入运算的重载

void main()
{
complex complex1;
cin > > complex1;
}
[/Quote]

jieao111 2008-03-09
• 打赏
• 举报

``````
istream &operator > >  (istream &in,complex &s)
{
cout  < < "请输入实部的值： " ;
in > >  s.real ; //cin应该改成in
cout  < < "请输入虚部的值： " ;
in > >  s.image;
return in;
}
//定义的复数类的输入运算的重载

void main()
{
complex complex1;
cin > >  complex1;
}
``````
nanyou1 2008-03-08
• 打赏
• 举报

sinosinux 2008-03-05
• 打赏
• 举报

gcc编译，运行都没错误

``````#include <iostream>

using namespace std;
class complex
{
public:
int real;
int image;
};
istream &operator >>  (istream &in,complex &s)
{
cout  << "请输入实部的值： " ;
cin >>  s.real ;
cout  << "请输入虚部的值： " ;
cin >>  s.image;
return in;
}

int main()
{
complex complex1;
cin >>  complex1;
return 0;
} ``````

6.3w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等
c++ 技术论坛（原bbs）

2008-03-05 07:12

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下