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

今天又碰到这个问题,由于以前没有记笔记的习惯,所以碰到这个问题之后纠结了很久。友元函数本来就是给那些既需要访问类成员而又不能作为相关类的成员的函数或者类来访问类私有变量的方法。从这儿可以看出,友元函数会破坏类的封装性,所以还是少用为妙。

  1. #include "iostream"  
  2. using namespace std;  
  3. class MyClass  
  4. {  
  5.   
  6. public:  
  7.     double val;  
  8.     MyClass(){a = b = 0;}  
  9.     MyClass(int x, int y)  
  10.     {  
  11.         a = x;  
  12.         b = y;  
  13.         val = 0.0;  
  14.     }  
  15.     ~MyClass()  
  16.     {  
  17.         cout << "Destructing MyClass(" << a << ", " << b << ")" << endl;  
  18.     }  
  19.     int sum()  
  20.     {  
  21.         return a + b;  
  22.     }  
  23.     friend ostream &operator<<(std::ostream &strm, const MyClass &obj);  
  24. private:  
  25.         int a, b;  
  26. };  
  27. ostream &operator<<(ostream &strm, const MyClass &obj)//  
  28. {  
  29.     strm << "(" << obj.a << " " << obj.b << ")";  
  30.     return strm;  
  31. }  
  32. int main(){  
  33.   
  34.   
  35.     return 0;  
  36. }  
当我在VC6.0上像这样写代码时,编译器报如下错误:

  1. Compiling...  
  2. test.cpp  
  3. D:\VCProject\FriendTest\test.cpp(33) : error C2248: 'a' : cannot access private member declared in class 'MyClass'  
  4.         D:\VCProject\FriendTest\test.cpp(29) : see declaration of 'a'  
  5. D:\VCProject\FriendTest\test.cpp(33) : error C2248: 'b' : cannot access private member declared in class 'MyClass'  
  6.         D:\VCProject\FriendTest\test.cpp(29) : see declaration of 'b'  
上网一查,发现这是VC6.0的一个经典BUG,是VC6.0对友元函数的支持不够,同时跟namespace也有关系。

于是,有两种方式可以解决这个问题:

方式一:注释掉 using namespace std;加上如下声明:

  1. using std::cout;  
  2. using std::endl;  
  3. using std::ostream;  
完整代码如下:

  1. #include "iostream"  
  2. //using namespace std;  
  3. using std::cout;  
  4. using std::endl;  
  5. using std::ostream;  
  6.   
  7. class MyClass  
  8. {  
  9.   
  10. public:  
  11.     double val;  
  12.     MyClass(){a = b = 0;}  
  13.     MyClass(int x, int y)  
  14.     {  
  15.         a = x;  
  16.         b = y;  
  17.         val = 0.0;  
  18.     }  
  19.     ~MyClass()  
  20.     {  
  21.         cout << "Destructing MyClass(" << a << ", " << b << ")" << endl;  
  22.     }  
  23.     int sum()  
  24.     {  
  25.         return a + b;  
  26.     }  
  27.     friend ostream &operator<<(std::ostream &strm, const MyClass &obj);  
  28. private:  
  29.         int a, b;  
  30. };  
  31. ostream &operator<<(ostream &strm, const MyClass &obj)//  
  32. {  
  33.     strm << "(" << obj.a << " " << obj.b << ")";  
  34.     return strm;  
  35. }  
  36. int main(){  
  37.   
  38.   
  39.     return 0;  
  40. }  

方法二:在程序中所以用的比如,ostream、istream、endl 等等std中的关键字前面都加上std::。比如:std::ostream,std::istream,std::endl;

点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值