在继承时重载>>和<<时出现问题?

elegant87 2008-08-07 05:04:14
我写了一个继承的类,在不同的类中对>>和<<共进行了三次重载,
运算符重载实质就是函数重载,可以识别的,但并没有达到我的目的。
大家帮忙看看吧!
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

class Birthday
{
public:
Birthday();
Birthday(const Birthday& b);
friend istream& operator >>(istream& in,Birthday& b);
friend ostream& operator <<(ostream& out,Birthday& b);
private:
size_t year;
size_t month;
size_t day;
};

class People
{
public:
People();
People(const People& p);
friend istream& operator >>(istream& in,People& p);
friend ostream& operator <<(ostream& out,People& p);
protected:
string name;
Birthday bir;
};
class Student: public People
{
public:
Student();
Student(const Student& s);
friend istream& operator >>(istream& in,Student& s);
friend ostream& operator <<(ostream& out,Student& s);
private:
size_t score;
};
Birthday::Birthday(){}

Birthday::Birthday(const Birthday& b):year(b.year),month(b.month),day(b.day){}

istream& operator >>(istream& in,Birthday& b)
{
cout<<"Enter the birthday(year/month/day): ";
in>>b.year>>b.month>>b.day;
return in;
}

ostream& operator <<(ostream& out,Birthday& b)
{
out<<b.year<<"/"<<b.month<<"/"<<b.day<<endl;
return out;
}

People::People(){}

People::People(const People& p):name(p.name),bir(p.bir){}

istream& operator >>(istream& in,People& p)
{
cout<<"Enter the name: ";
in>>p.name;
in>>p.bir;
return in;
}
ostream& operator <<(ostream& out,People& p)
{
out<<"The name is: "<<p.name<<endl;
out<<"The birthday is: "<<p.bir;
return out;
}

Student::Student(){}

Student::Student(const Student& s):People(s),score(s.score){}

istream& operator >>(istream& in,Student& s)
{
cout<<"Enter the name: ";
in>>s.name;
in>>s.bir;
cout<<"Enter the score: ";
in>>s.score;
return in;
}

ostream& operator <<(ostream& out,Student& s)
{
out<<"The name is: "<<s.name<<endl;
out<<"The birthday is: "<<s.bir;
out<<"The score is: "<<s.score<<endl;
return out;
}

void Save(People &p,Student& s)
{
ofstream outfile;
outfile.open("people.txt",ios::out);
if(!outfile.is_open())
{
cerr<<"Can not open the file!"<<endl;
exit(0);
}
outfile<<p<<endl<<"Stuent message is:"<<endl<<s;
cout<<"Save end!"<<endl;
}

int main()
{
People p;
cin>>p;
cout<<p;
Student s;
cin>>s;
cout>>s;
Save(p,s);
system("pause");
return 0;
}

错误提示:
Compiling...
145.cpp
145.cpp: In function `int main()':
145.cpp:118: error: no match for 'operator>>' in 'std::cout >> s'
145.cpp:46: error: candidates are: std::istream& operator>>(std::istream&,
Birthday&)
145.cpp:63: error: std::istream& operator>>(std::istream&,
People&)
145.cpp:81: error: std::istream& operator>>(std::istream&,
Student&)

145.o - 4 error(s), 0 warning(s)
...全文
89 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangdeqie 2008-08-07
  • 打赏
  • 举报
回复
楼主比我本科那会儿强多了,现在读研,编程能力稍微有点提高!
wangdeqie 2008-08-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 elegant87 的回复:]
小弟新手,正在看C++Primer,有什么好的意见提提!谢谢大家,我会努力的!
[/Quote]
俺也是新手菜鸟,共同进步,互相勉励下^_^
elegant87 2008-08-07
  • 打赏
  • 举报
回复
小弟新手,正在看C++Primer,有什么好的意见提提!谢谢大家,我会努力的!
xkyx_cn 2008-08-07
  • 打赏
  • 举报
回复
很好,继续努力

[Quote=引用 6 楼 elegant87 的回复:]
大家评价一下这个程序怎么样?
[/Quote]
Rocky_ 2008-08-07
  • 打赏
  • 举报
回复
学会根据编译器提示自己修改错误
编译器有提出多少行的
一般编译错误是语法错误
很容易修改
elegant87 2008-08-07
  • 打赏
  • 举报
回复
大家评价一下这个程序怎么样?
elegant87 2008-08-07
  • 打赏
  • 举报
回复
呵呵,谢谢大家的提醒。我真是太粗心了!
jay的Fans 2008-08-07
  • 打赏
  • 举报
回复
Student s;
cin>>s;
cout>>s;//好奇怪...
Save(p,s);
xkyx_cn 2008-08-07
  • 打赏
  • 举报
回复
呵呵,编程要细心:

cout << s;
wangdeqie 2008-08-07
  • 打赏
  • 举报
回复

//改成这样
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

class Birthday
{
public:
Birthday();
Birthday(const Birthday& b);
friend istream& operator >>(istream& in,Birthday& b)
{
cout<<"Enter the birthday(year/month/day): ";
in>>b.year>>b.month>>b.day;
return in;
}
friend ostream& operator <<(ostream& out,Birthday& b)
{
out<<b.year<<"/"<<b.month<<"/"<<b.day<<endl;
return out;

}
private:
size_t year;
size_t month;
size_t day;
};

class People
{
public:
People();
People(const People& p);
friend istream& operator >>(istream& in,People& p)
{
cout<<"Enter the name: ";
in>>p.name;
in>>p.bir;
return in;
}
friend ostream& operator <<(ostream& out,People& p)
{
out<<"The name is: "<<p.name<<endl;
out<<"The birthday is: "<<p.bir;
return out;
}
protected:
string name;
Birthday bir;
};
class Student: public People
{
public:
Student();
Student(const Student& s);
friend istream& operator >>(istream& in,Student& s)
{
cout<<"Enter the name: ";
in>>s.name;
in>>s.bir;
cout<<"Enter the score: ";
in>>s.score;
return in;
}
friend ostream& operator <<(ostream& out,Student& s)
{
out<<"The name is: "<<s.name<<endl;
out<<"The birthday is: "<<s.bir;
out<<"The score is: "<<s.score<<endl;
return out;
}
private:
size_t score;
};
Birthday::Birthday(){}

Birthday::Birthday(const Birthday& b):year(b.year),month(b.month),day(b.day){}


People::People(){}

People::People(const People& p):name(p.name),bir(p.bir){}


Student::Student(){}

Student::Student(const Student& s):People(s),score(s.score){}

void Save(People &p,Student& s)
{
ofstream outfile;
outfile.open("people.txt",ios::out);
if(!outfile.is_open())
{
cerr<<"Can not open the file!"<<endl;
exit(0);
}
outfile<<p<<endl<<"Stuent message is:"<<endl<<s;
cout<<"Save end!"<<endl;
}

int main()
{
People p;
cin>>p;
cout<<p;
Student s;
cin>>s;
cout<<s;
Save(p,s);
system("pause");
return 0;
}

ttkk_2007 2008-08-07
  • 打赏
  • 举报
回复
cout>>s;
很明显,写错了,cout << s;

64,646

社区成员

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

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