关于重载>>

h248080441 2010-07-23 01:34:53
#include "complex0.h"
#include <iostream>
complex::complex()
{
x=0;
y=0;
}
complex::complex(double a,double b)
{
x=a;
y=b;
}
complex complex::operator+ (const complex &t)const
{
complex d;
d.x=x+t.x;
d.y=y+t.y;
return d;
}
complex complex::operator- (const complex &t)const
{
complex d;
d.x=x-t.x;
d.y=y-t.y;
return d;
}
complex complex::operator* (const complex &t)const
{
complex d;
d.x=x*t.x-y*t.y;
d.y=x*t.y+y*t.x;
return d;
}
complex operator* (const complex &t,double c)
{
complex d;
d.x=c*t.x;
d.y=c*t.y;
return d;
}
complex operator~ (const complex &t)
{
complex d;
d.x=t.x;
d.y=-t.y;
return d;
}
std::ostream & operator<<(std::ostream & os,const complex &t)
{
os<<"("<<t.x<<","<<t.y<<"i"<<")";
return os;
}
std::istream & operator>>(std::istream & is,const complex &t)
{
is>>t.x>>t.y;
return is;
}



这是我的代码,出现的错误是error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'const double' (or there is no acceptable conversion)
求指点
还有就是我把
std::istream & operator>>(std::istream & is,const complex &t)
{
is>>t.x>>t.y;
return is;
}
换成std::istream & operator>>(std::istream & is,const complex &t)
{
cout<<"real is:";
is>>t.x
cout<<"imag is:";
is>>t.y;
return is;
}
为什么会有个错误'cout' : undeclared identifier。这是怎么产生的呢,求指导
...全文
135 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
黑泡泡选手 2010-07-24
  • 打赏
  • 举报
回复
回复真快,我都插不上嘴!
cxxer 2010-07-24
  • 打赏
  • 举报
回复
using namespace std;
elegant87 2010-07-23
  • 打赏
  • 举报
回复
直接加上命名空间就行了
using namespace std
yxqyrh 2010-07-23
  • 打赏
  • 举报
回复
你要输出,值肯定要改变,不能用const
h248080441 2010-07-23
  • 打赏
  • 举报
回复
好的,解决了。多谢各位
失落的凡凡 2010-07-23
  • 打赏
  • 举报
回复
>> 的对象怎么可能是一个const &呢?
pengzhixi 2010-07-23
  • 打赏
  • 举报
回复
用std::cout
pengzhixi 2010-07-23
  • 打赏
  • 举报
回复
std::istream & operator>>(std::istream & is, complex &t)//去掉const

t
taodm 2010-07-23
  • 打赏
  • 举报
回复
std::cout

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧