到底哪出了问题? 谁来帮帮我啊,郁闷啊
以下程序究竟哪出了问题,谁来帮帮我,谢了!
#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.
问题点数:0、回复次数:6Top
1 楼Andy84920(你也不懂)回复于 2004-12-02 12:48:32 得分 0
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;
}
Top
2 楼avalonBBS("︶.︶メ)→( ̄ε ̄メ)回复于 2004-12-02 12:55:54 得分 0
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;
}Top
3 楼kenner_2(肯~小海洋)回复于 2004-12-02 12:56:30 得分 0
这样可以嘛?
我试试,别走啊,谢了Top
4 楼kenner_2(肯~小海洋)回复于 2004-12-02 12:59:29 得分 0
可以了啊,谢谢啊,这到底为什么呢?
能给详讲一下嘛?Top
5 楼kenner_2(肯~小海洋)回复于 2004-12-02 13:01:24 得分 0
为什么给定义成友元函数问题就消失了呢?
不盛感激Top
6 楼greenteanet(扎扎实实打基础,保持一颗平常心。)回复于 2004-12-02 13:10:16 得分 0
#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;
}Top




