CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

关于友元函数的问题

楼主cumtjianbolu(阿波罗)2004-12-03 14:44:49 在 C/C++ / C++ 语言 提问

各位大虾,友元函数是不是不能放在类外实现啊。为什么我每次把友元函数放在类外实现,都编译时都会产生错误能。比如说我这样一段代码:#ifndef   stH  
  #define   stH  
  #include<iostream>  
  using   namespace   std;  
  class   stack  
  {  
  private:  
  static   int   maxsize;  
  int   top;  
  int   *data;  
  public:  
  stack();  
  void   push(int   item);  
  int   pop();  
  int   peek();  
  void   stackEmpty()const;  
  void   stackFull()const;  
  friend   ostream   &operator<<(ostream   &out,stack   &st);  
   
  };  
  #endif  
  如果我把ostream   &operator<<(ostream   &out,stack   &st);    
  放到类外实现就会产生错误。这个文件我是放在一个保存在一个头文件中的。我实现的时候放在了一个  
  .CPP文件中,这样可以吗?  
  问题点数:0、回复次数:2Top

1 楼zhouyong0371(小周周)回复于 2004-12-03 15:15:56 得分 0

#include<iostream>  
  using   namespace   std;  
   
  class   stack  
  {  
  private:  
  static   int   maxsize;  
  int   top;  
  int   *data;  
  public:  
  stack(){  
  top   =   0;  
  data   =   new   int[11];  
  };  
   
  ~stack(){  
  delete   []   data;  
  };  
   
  void   push(int   item){  
  *(data   +   top)   =   item;  
  top++;  
  };  
   
  int   pop(){    
  top--;  
  return   *(data   +   top);  
  };  
   
  int   peek(){  
  return   *(data   +   top   -   1);  
  };  
  void   stackEmpty()const{  
  };  
  void   stackFull()const{  
  };  
  //friend   ostream   &operator<<(ostream   &out,stack   &st);  
   
  };  
   
  int   stack::maxsize   =   10;  
   
  ostream   &operator<<(ostream   &out,   stack   &st)  
  {  
  return   out   <<   st.peek();  
  }  
   
  void   main()  
  {  
  stack   s;  
  s.push(10);  
  cout   <<   s   <<   endl;  
  }Top

2 楼wwxsoft(婉儿)回复于 2004-12-03 15:43:24 得分 0

友元函数可以放到类外来实现得,比如下面一个程序,就是友元函数放到类外来实现得  
  #include   <iostream>  
  #include   <stdlib.h>  
   
  using   namespace   std;  
  class   complex  
  {  
  public:  
          complex(double   r=0.0,double   i=0.0){real=r;imag=i;}  
          friend   complex   operator   +(complex   c1,complex   c2);  
          friend   complex   operator   -(complex   c1,complex   c2);  
          void   display();  
  private:  
          double   real;  
          double   imag;  
  };  
  void   complex::display()  
  {  
  cout<<"("<<real<<","<<imag<<")"<<endl;  
  }  
  complex   operator   +(complex   c1,complex   c2)  
  {  
  return   complex(c1.real+c2.real,c1.imag+c2.imag);  
  }  
  complex   operator   -(complex   c1,complex   c2)  
  {  
  return   complex(c1.real-c2.real,c1.imag-c2.imag);  
  }  
  int   main(int   argc,   char   *argv[])  
  {  
      complex   c1(5,4),c2(2,10),c3;  
      cout<<"c1=";c1.display();  
      cout<<"c2=";c2.display();  
      c3=c1+c2;  
      cout<<"c3=c1+c2=";c3.display();  
      c3=c1-c2;  
      cout<<"c3=c1-c2";c3.display();  
      system("PAUSE");  
      return   0;  
  }Top

相关问题

  • 友元函数
  • 友元函数、成员函数...
  • 关于友元函数
  • 友元函数问题?
  • 友元函数的问题
  • 友元函数问题。
  • 关于友元函数
  • 友元函数问题
  • 友元函数能否继承
  • 友元函数的一个问题!

关键词

  • 函数
  • top
  • complex
  • stack
  • friend
  • double
  • operator
  • using namespace std
  • const
  • item

得分解答快速导航

  • 帖主:cumtjianbolu

相关链接

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

广告也精彩

反馈

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