CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

重载的问题,很有意思

楼主okdavinci(apple.davinci)2005-05-17 20:28:51 在 C/C++ / C++ 语言 提问

下定义了一个学生类,通过重载operator<<和operator>>实现对student的直接输入输出操作  
  #ifndef   STUDENT_H  
  #define   STUDENT_H  
  #include"action.h"  
  #include<iostream>  
  #include<string>  
   
  using   std::string;  
  using   std::istream;  
  using   std::ostream;  
   
  class   student  
  {  
  friend   class   action;  
  friend   ostream&   operator<<(   ostream&   ,   student&);  
  friend   istream&   operator>>(   istream&,   student&);  
  public:  
  student();  
  student(const   student&);  
  student   &operator=(const   student&);  
  //有没有必要提供一个destructor??  
   
  int   getid()const;  
  double   getgrade()const;  
  string   getname()const;  
  //void   input(const   string&   ,const   int&,const   double&);  
  private:  
  int   id;  
  string   name;  
  double   grade;  
  string   classname;   //班级名称  
  };  
   
  //typedef   vector<student>   myclass;  
   
  下面是实现:  
   
  #include<iostream>  
  #include<vector>  
  #include<string>  
  #include"student.h"  
  #include"action.h"  
  #include"global.h"  
  using   namespace   std;  
   
   
  ''''''  
  ''''  
  '''  
  ''''//  
   
  /*****************************************************************************************/  
   
  //对友元函数的实现  
  istream&   operator>>(istream&   is,student&   stu)  
  {  
  cout<<"operator<<   grade,id,name"<<endl;  
  cin>>stu.id;  
  cin>>stu.grade;  
  cin>>stu.name;  
  cin>>stu.classname;//输入所在地班级名字  
  return   is;  
   
  }  
  ostream&   operator<<(   ostream&   os,student&   stu   )  
  {  
   
  cout<<"id:"<<stu.getid()<<endl;  
  cout<<"name:"<<stu.getname()<<endl;  
  cout<<"grades:"<<stu.getgrade()<<endl;  
  return   os;  
  }  
   
   
  为什么我在myclass(班级类)中重载输出操作符的时候出现问题;  
  ostream&   operator<<(ostream&   os,const   myclass&   class1)     //对一个班级的输出  
  {  
  cout<<"这个班级的信息"<<endl;  
  vector<student>::const_iterator   iter;  
   
  for(iter=class1.getclass().begin();iter!=class1.getclass().end();iter++)  
  {  
  cout<<*iter<<endl;  
  }  
  return   os;  
  }  
  问题说   输出操作<<没有找到接受“<未知>”类型的右操作数的运算符(或没有可接受的转换)  
  .*iter不就是student类型的吗?我已经重载了student的输出操作符为什么cout<<*iter<<endl;  
  会不行那"?????  
   
   
   
   
   
   
   
  #endif 问题点数:10、回复次数:11Top

1 楼zsh6709(世界上没有后悔药吃的!!!)回复于 2005-05-17 21:21:51 得分 0

有错误啊,当然....  
  其实我也不大懂,现在正在学它,可书上说的也很含糊!1Top

2 楼maxuming914a1(马克)回复于 2005-05-17 21:44:51 得分 0

upTop

3 楼leconte(leconte)回复于 2005-05-17 22:06:08 得分 10

 
  你所有重载<<   >>的函数里面os都没有用到,而用cout,cin。这是不正确的。  
  例如ostream&   operator<<(   ostream&   os,student&   stu   )  
  {  
   
  cout<<"id:"<<stu.getid()<<endl;  
  cout<<"name:"<<stu.getname()<<endl;  
  cout<<"grades:"<<stu.getgrade()<<endl;  
  return   os;  
  }  
  应改为  
  ostream&   operator<<(   ostream&   os,student&   stu   )  
  {  
   
  os<<"id:"<<stu.getid()<<endl;  
  os<<"name:"<<stu.getname()<<endl;  
  os<<"grades:"<<stu.getgrade()<<endl;  
  return   os;  
  }  
  Top

4 楼dongyi940333(科斯塔)回复于 2005-05-17 22:25:47 得分 0

你应该将友元函数istream&   operator>>中的cin改为is,将友元函数ostream&   operator<<中的cout改为osTop

5 楼nicknide(封月翔天)回复于 2005-05-17 22:37:17 得分 0

vector<student>::const_iterator   iter;  
  ostream&   operator<<(   ostream&   os,student&   stu   );  
   
  关键错误所在,自己比较这2行。  
  Top

6 楼nicknide(封月翔天)回复于 2005-05-17 22:44:48 得分 0

还有,你这个问题的标题太弱智了,进来就是想说这个,为什么不这么问:  
   
  UDT   重载友元   operator   <<   (ostream,UDT),使用时类型推导失败的问题?  
   
  问得弱智,高手自然不会来...Top

7 楼okdavinci(apple.davinci)回复于 2005-05-18 10:22:51 得分 0

vector<student>::const_iterator   iter;  
  ostream&   operator<<(   ostream&   os,student&   stu   );  
   
  关键错误所在,自己比较这2行。  
   
  可以   说的具体点吗?Top

8 楼nicknide(封月翔天)回复于 2005-05-18 21:49:11 得分 0

可以问一下你看什么书学的C++吗?  
  如果是国内的书,建议扔掉,然后换国外的。  
   
  如果是国外的书,看看有没有介绍C++中蕴含的思想和习惯,如果没有讲,也可以扔了  
   
  上面那个问题,如果你使用惯用法编程的话,是不会出现这个情况的,因为你没有遵照规则。  
  因此再仔细看看书吧,或者好好思考一下,这个不是重载的问题,而是一个限定符的问题(我说的很明白了吧)  
   
  Top

9 楼okdavinci(apple.davinci)回复于 2005-05-19 08:39:02 得分 0

我知道了是ostream&   operator<<(   ostream&   os,student&   stu   );改为ostream&   operator<<(   ostream&   os,const   student&   stu   );因为有const_iterator是吗?Top

10 楼nicknide(封月翔天)回复于 2005-05-19 10:39:09 得分 0

楼上正解...Top

11 楼CPPLOVER_78(CPP爱好者)回复于 2005-05-19 12:09:04 得分 0

同意okdavinci   的答案.同意   nicknide(封月翔天)   的建议Top

相关问题

  • 很有意思的问题!!
  • 重载()
  • 重载!
  • 一段很有意思的影片~~
  • 两个很有意思的问题
  • 转一篇很有意思的文章
  • 一个很有意思的问题!
  • 一个很有意思的问题
  • 一个很有意思的小问题!!
  • 迷惑,很有意思的问题

关键词

  • 函数
  • student
  • ostream
  • stu
  • iter
  • istream
  • cin
  • operator
  • const
  • std

得分解答快速导航

  • 帖主:okdavinci
  • leconte

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo