这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » VC6.0中友元函数无法访问类私有成员的解决办法-------VC6.0的bug

共1条 1/1 1 跳转至

VC6.0中友元函数无法访问类私有成员的解决办法-------VC6.0的bug

助工
2014-10-05 20:26:28     打赏

举个例子:


 





  1. #include<iostream> 
  2. using namespace std; 
  3. class cylinder 

  4.     friend istream operator>>(istream& is,cylinder &cy); 
  5. public:     
  6.     inline double square() 
  7.     {       return length*(width+height)*2+width*height*2;    } 
  8.     inline double volume() 
  9.     {      return length*width*height;      } 
  10. private: 
  11.     double length; 
  12.     double width; 
  13.     double height; 
  14. }; 
  15. istream operator>>(istream is,cylinder &cy) 

  16.     cout<<"input length:"<<endl; 
  17.     is>>cy.length; 
  18.     cout<<"input width:"<<endl; 
  19.     is>>cy.width; 
  20.     cout<<"input height:"<<endl; 
  21.     is>>cy.height; 
  22.     return is; 

  23. int main() 

  24.     cylinder first; 
  25.     cin>>first; 
  26.     cout<<first.square()<<endl; 
  27.     cout<<first.volume()<<endl; 
  28.     return 0; 
  29. }



这些代码在VC6.0中不能被编译通过:提示不能访问私有成员,没有这个访问权限


改成这样就可以了,代码如下:


 





  1. #include<iostream> 
  2. using std::cin; 
  3. using std::endl; using std::cout; 
  4. using std::ostream; 
  5. using std::istream; 
  6. using std::ostream; 
  7. class cylinder 

  8.     friend istream operator>>(istream& is,cylinder &cy); 
  9. public:     
  10.     inline double square() 
  11.     {       return length*(width+height)*2+width*height*2;    } 
  12.     inline double volume() 
  13.     {      return length*width*height;      } 
  14. private: 
  15.     double length; 
  16.     double width; 
  17.     double height; 
  18. }; 
  19. istream operator>>(istream is,cylinder &cy) 

  20.     cout<<"input length:"<<endl; 
  21.     is>>cy.length; 
  22.     cout<<"input width:"<<endl; 
  23.     is>>cy.width; 
  24.     cout<<"input height:"<<endl; 
  25.     is>>cy.height; 
  26.     return is; 

  27. int main() 

  28.     cylinder first; 
  29.     cin>>first; 
  30.     cout<<first.square()<<endl; 
  31.     cout<<first.volume()<<endl; 
  32.     return 0; 




原因:


这据说是VC的一个经典BUG。和namespace也有关.


只要含有using namespace std; 就会提示友员函数没有访问私有成员的权限。


解决方法:去掉using namespace std;换成更小的名字空间。


例如:


含有#include <string>就要加上using std::string


含有#include <fstream>就要加上using std::fstream


含有#include <iostream>就要加上using std::cin; using std::cout; using std::ostream; using std::istream; using std::endl; 等等,需要什么即可通过using声明什么.


 


下面给出流浪给的解决办法:


//方法一:


//提前声明


class cylinder;


istream &operator>>(istream& is,cylinder &cy);


//方法二:


//不用命名空间 或者 像晨雨那样写


#include<iostream.h>


 


 


//方法三:


class cylinder


{


    friend istream &operator>>(istream& is,cylinder &cy)//写在类里面


    {


        cout<<"input length:"<<endl;


        is>>cy.length;


        cout<<"input width:"<<endl;


        is>>cy.width;


        cout<<"input height:"<<endl;


        is>>cy.height;


        return is;


       


    }


..........


//方法四:打SP6补丁,貌似不好使。。。(呵呵,是貌似也没用)


 


//方法五:换别的对标准C++支持好的编译器,如DEV C++/。。。


共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]