新微赢技术网
标题:
[求助]重载问题
[打印本页]
作者:
鰰﹎話
时间:
2009-11-3 02:17
标题:
[求助]重载问题
#include<iostream>
#include<string>
using namespace std;
class person{
private:
string name,address;
char sex;
int ID;
public:
person(){name="king";address="09109";sex='M';ID=109;}
person(string name1,string address1,char sex1,int ID1);
void New_set();
void out_message();
friend ostream& operator<<(ostream& stream,person& p);
friend istream& operator>>(istream& stream,person& p);
};
void person::out_message()
{
cout<<"姓 名:"<<name<<endl;
cout<<"性 别:"<<sex<<endl;
cout<<"地 址:"<<address<<endl;
cout<<"身份证号:"<<ID<<endl;
}
person::person(string name1,string address1,char sex1,int ID1)
{
name=name1;
address=address1;
sex=sex1;
ID=ID1;
}
void person::New_set()
{
cout<<"请输入姓名:";
cin>>name;
cout<<"请输入地址:";
cin>>address;
cout<<"请输入性别:";
cin>>sex;
cout<<"请输入身份证号:";
cin>>ID;
}
ostream& operator<<(ostream& stream,person& p)
{
p.out_message();
return stream;
}
istream& operator>>(istream& stream,person& p)
{
p.New_set();
return stream;
}
void main()
{
person A;
cout<<A<<endl;
cout<<"请输入更新后的信息:"<<endl;
cin>>A;
cout<<A<<endl;
}
运行结果:
--------------------Configuration: Cpp2 - Win32 Debug--------------------
Compiling...
Cpp2.cpp
E:\C++\I-O流实验\Cpp2.cpp(63) : error C2593: 'operator <<' is ambiguous
E:\C++\I-O流实验\Cpp2.cpp(65) : error C2593: 'operator >>' is ambiguous
E:\C++\I-O流实验\Cpp2.cpp(66) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.
Cpp2.exe - 3 error(s), 0 warning(s)
哪个大虾帮忙看下撒
以前都没问题的啊~~~
作者:
紫禁之颠
时间:
2009-11-3 02:17
以下是引用weikie在2006-5-23 21:22:00的发言:
#include<iostream>
#include<string>
using namespace std;
class person{
private:
string name,address;
char sex;
int ID;
public:
person(){name="king";address="09109";sex='M';ID=109;}
person(string name1,string address1,char sex1,int ID1);
void New_set();
void out_message();
friend ostream& operator<<(ostream& stream,person& p);
friend istream& operator>>(istream& stream,person& p);
};
void person::out_message()
{
cout<<"姓 名:"<<name<<endl;
cout<<"性 别:"<<sex<<endl;
cout<<"地 址:"<<address<<endl;
cout<<"身份证号:"<<ID<<endl;
}
person::person(string name1,string address1,char sex1,int ID1)
{
name=name1;
address=address1;
sex=sex1;
ID=ID1;
}
void person::New_set()
{
cout<<"请输入姓名:";
cin>>name;
cout<<"请输入地址:";
cin>>address;
cout<<"请输入性别:";
cin>>sex;
cout<<"请输入身份证号:";
cin>>ID;
}
ostream& operator<<(ostream& stream,person& p)
{
p.out_message();//流对象是stream,但是你这里是把数据插入到流对象cout中
return stream;//现在stream对象中,什么都没有
}
istream& operator>>(istream& stream,person& p)
{
p.New_set();//流对象是stream,但是你这里是把数据插入到流对象cin中
return stream;//现在stream对象中,什么都没有
}
void main()
{
person A;
cout<<A<<endl;
cout<<"请输入更新后的信息:"<<endl;
cin>>A;
cout<<A<<endl;
}
运行结果:
--------------------Configuration: Cpp2 - Win32 Debug--------------------
Compiling...
Cpp2.cpp
E:\C++\I-O流实验\Cpp2.cpp(63) : error C2593: 'operator <<' is ambiguous
E:\C++\I-O流实验\Cpp2.cpp(65) : error C2593: 'operator >>' is ambiguous
E:\C++\I-O流实验\Cpp2.cpp(66) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.
Cpp2.exe - 3 error(s), 0 warning(s)
哪个大虾帮忙看下撒
以前都没问题的啊~~~
作者:
遗失ぜ的ァ美
时间:
2009-11-3 02:17
#include<iostream.h>
class person{
private:
char name,address;
char sex;
int ID;
public:
person(){name='k';address='0';sex='M';ID=109;};
person(char name1,char address1,char sex1,int ID1);
void New_set(istream& stream);
void out_message(ostream& stream);
friend ostream& operator<<(ostream& stream,person& p);
friend istream& operator>>(istream& stream,person& p);
};
void person::out_message(ostream& stream)
{
stream<<"姓 名:"<<name<<endl;
stream<<"性 别:"<<sex<<endl;
stream<<"地 址:"<<address<<endl;
stream<<"身份证号:"<<ID<<endl;
}
person::person(char name1,char address1,char sex1,int ID1)
{
name=name1;
address=address1;
sex=sex1;
ID=ID1;
}
void person::New_set(istream& stream)
{
cout<<"请输入姓名:";
stream>>name;
cout<<"请输入地址:";
stream>>address;
cout<<"请输入性别:";
stream>>sex;
cout<<"请输入身份证号:";
stream>>ID;
}
ostream& operator<<(ostream& stream,person& p)
{
p.out_message(stream);
return stream;
}
istream& operator>>(istream& stream,person& p)
{
p.New_set(stream);
return stream;
}
void main()
{
try{
person A;
cout<<A;
cout<<"请输入更新后的信息:"<<endl;
cin>>A;
cout<<A<<endl;
}
catch(...)
{
cout<<"error";
}
}
大概是因为string不能重载吧.
用VS.2003可以编译.建议你用新的编译器.
作者:
梦缘o☆无恒
时间:
2009-11-3 02:18
友元重载函数声明和重载函数实现有二义性,可以通过下面两个方法解决:
1.在友元声明处实现函数,这样不会有二义性:
friend ostream& operator<<(ostream& stream,person& p)
{
p.out_message();
return stream;
}
friend istream& operator>>(istream& stream,person& p)
{
p.New_set();
return stream;
}
复制代码
2.在友元声明前声明一下重载函数的原型,避免造成二义性
class person;
ostream& operator<<(ostream& stream,person& p);
istream& operator>>(istream& stream,person& p);
class person{
private:
string name,address;
char sex;
int ID;
public:
person(){name="king";address="09109";sex='M';ID=109;}
person(string name1,string address1,char sex1,int ID1);
void New_set();
void out_message();
friend ostream& operator<<(ostream& stream,person& p);
friend istream& operator>>(istream& stream,person& p);
};
复制代码
作者:
轌婲の滿天飛
时间:
2009-11-3 02:18
to aogun:
原来是如此,领教了~!
作者:
ゞ↘Μc总督
时间:
2009-11-3 02:18
呵呵,等我学久了我就会解决了的
欢迎光临 新微赢技术网 (http://bbs.weiying.cn/)
Powered by Discuz! X3.2