到底哪出了问题? 谁来帮帮我啊,郁闷啊

kenner_2 2004-12-02 12:45:57
以下程序究竟哪出了问题,谁来帮帮我,谢了!
#include <iostream.h>
#include <istream.h>
#include <ostream.h>
#include <string.h>
class Worker{
public:
char name[20];
int id;
float wage;

istream& operator>>(istream& istr, Worker& x)
{
istr>>x.name>>x.id>>x.wage;
return istr;
}
ostream& operator<<(ostream& ostr, Worker& x)
{
ostr<<x.id<<" "<<x.name<<" "<<x.wage<<endl;
return ostr;
}
Worker(int i,char *nam,float wag)
{
id=i;
strcpy(name,nam);
wage=wag;
}
Worker()
{
}
};

void main()
{
Worker worker;
cin>>worker;
cout<<worker;
}



提示出错如下:
1. error C2804: binary 'operator >>' has too many parameters
2. error C2333: '>>' : error in function declaration; skipping function body
3. error C2804: binary 'operator <<' has too many parameters
4. error C2333: '<<' : error in function declaration; skipping function body
5. error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class Worker' (or there is no acceptable conversion)
6. error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Worker' (or there is no acceptable conversion)
Error executing cl.exe.
...全文
66 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kenner_2 2004-12-02
  • 打赏
  • 举报
回复
可以了啊,谢谢啊,这到底为什么呢?
能给详讲一下嘛?
kenner_2 2004-12-02
  • 打赏
  • 举报
回复
这样可以嘛?
我试试,别走啊,谢了
avalonBBS 2004-12-02
  • 打赏
  • 举报
回复
class Worker{
public:
char name[20];
int id;
float wage;


Worker(int i,char *nam,float wag)
{
id=i;
strcpy(name,nam);
wage=wag;
}
Worker(){}
};
istream& operator>>(istream & istr,Worker & x)
{
istr>>x.name>>x.id>>x.wage;
return istr;
}
ostream& operator<<(ostream& ostr, Worker& x)
{
ostr<<x.id<<" "<<x.name<<" "<<x.wage<<endl;
return ostr;
}
Andy84920 2004-12-02
  • 打赏
  • 举报
回复
friend istream& operator>>(istream& istr, Worker& x)
{
istr>>x.name>>x.id>>x.wage;
return istr;
}
friend ostream& operator<<(ostream& ostr, Worker& x)
{
ostr<<x.id<<" "<<x.name<<" "<<x.wage<<endl;
return ostr;
}
greenteanet 2004-12-02
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <istream.h>
#include <ostream.h>
#include <string.h>
class Worker
{
public:
char name[20];
int id;
float wage;

Worker(int i,char *nam,float wag)
{
id=i;
strcpy(name,nam);
wage=wag;
}
Worker() : id(0), wage(0)
{
}

friend istream& operator>>(istream& istr, Worker& x) ;
friend ostream& operator<<(ostream& ostr, Worker& x);
};

istream& operator>>(istream& istr, Worker& x)
{
istr>>x.id;
istr>>x.name;
istr>>x.wage;
return istr;
}
ostream& operator<<(ostream& ostr, Worker& x)
{
ostr<<x.id<<" "<<x.name<<" "<<x.wage<<endl;
return ostr;
}
void main()
{
Worker worker;
cin>>worker;
cout<<worker;
}
kenner_2 2004-12-02
  • 打赏
  • 举报
回复
为什么给定义成友元函数问题就消失了呢?
不盛感激

64,645

社区成员

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

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