重载输入输出的二义问题!

zhudonhua 2005-10-09 10:44:00
//:Student.h
#include<string.h>
class Student
{
public:
char * name;
int age;
char sex;
public:
Student(char* pname="no name",int n=20,char ch='f');
friend istream & operator >>(istream& in,Student& stu);
friend ostream & operator <<(ostream& out,const Student& stu);
};
Student::Student(char* pname,int n,char ch)
{
name=new char[strlen(pname)+1];
strcpy(name,pname);
age = n;
sex =ch;
}
istream & operator >>(istream & in,Student& stu)
{
in>>stu.name>>stu.age>>stu.sex;
return in;
}
ostream & operator <<(ostream & out,const Student& stu)
{
out<<"Name: "<<stu.name<<"\tAge: "<<stu.age<<"\tSex: "<<stu.sex<<endl;
return out;
}

#include<iostream>
using namespace std;
#include "Student.h"
int main()
{
Array<Student> stu(3);
for(int i =0 ;i<3;i++)
cin>>stu[i];
for(int j=0;j<3;j++)
cout<<stu[j];
system("PAUSE");
return EXIT_SUCCESS;
}

VC下编译竟然说有二义性!
不知道为何???
--------------------Configuration: ArrayTest - Win32 Debug--------------------
Compiling...
main.cpp
C:\Test\ArrayTest\main.cpp(17) : error C2593: 'operator >>' is ambiguous
C:\Test\ArrayTest\main.cpp(19) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.

ArrayTest.exe - 2 error(s), 0 warning(s)

...全文
79 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
漂流的代码 2005-10-10
  • 打赏
  • 举报
回复
直接在声明地方写就可以了,大概是vc6对标准支持不够
friend istream & operator >>(istream& in,Student& stu)
{
in>>stu.name>>stu.age>>stu.sex;
return in;
}
friend ostream & operator <<(ostream& out,const Student& stu)
{
out<<"Name: "<<stu.name<<"\tAge: "<<stu.age<<"\tSex: "<<stu.sex<<endl;
return out;
}
xlsue 2005-10-10
  • 打赏
  • 举报
回复
vc6如果打sp6应该没有问题。
zhudonhua 2005-10-10
  • 打赏
  • 举报
回复
是VC6.0的,Array是数组模板类

不行的,在Devc下顺利通过!

不知道为什么???
v41dugu 2005-10-09
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
改成#include <iostream.h>试试
v41dugu 2005-10-09
  • 打赏
  • 举报
回复
Array是???
v41dugu 2005-10-09
  • 打赏
  • 举报
回复
你6.0?

64,637

社区成员

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

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