C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

常用链接

留言簿(8)

搜索

  •  

最新评论

阅读排行榜

评论排行榜

转自:http://hi.baidu.com/%C4%CF%B7%C9%D1%E3%D1%E3%C4%CF%B7%C9/blog/item/4722d43c53868b09bba1672e.html

举个例子:

#include<iostream> 
using namespace std; 
class cylinder 

    friend istream 
operator>>(istream& is,cylinder &cy); 
public:     
    inline 
double square() 
    
{       return length*(width+height)*2+width*height*2;    } 
    inline 
double volume() 
    
{      return length*width*height;      } 
private
    
double length; 
    
double width; 
    
double height; 
}

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
}
 
int main() 

    cylinder first; 
    cin
>>first; 
    cout
<<first.square()<<endl; 
    cout
<<first.volume()<<endl; 
    
return 0
}

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

改成这样就可以了,代码如下:(我用的这个方法,但是得把变量改成public才行)

#include<iostream> 
using std::cin; 
using std::endl; using std::cout; 
using std::ostream; 
using std::istream; 
using std::ostream; 
class cylinder 

    friend istream 
operator>>(istream& is,cylinder &cy); 
public:     
    inline 
double square() 
    
{       return length*(width+height)*2+width*height*2;    } 
    inline 
double volume() 
    
{      return length*width*height;      } 
private
    
double length; 
    
double width; 
    
double height; 
}

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
}
 
int main() 

    cylinder first; 
    cin
>>first; 
    cout
<<first.square()<<endl; 
    cout
<<first.volume()<<endl; 
    
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++/。。。(呵呵)

本文来自CSDN博客,转载:http://blog.csdn.net/zgjxwl/archive/2008/10/13/3067973.aspx

posted on 2010-02-15 02:56 烟皑 阅读(1021) 评论(1)  编辑 收藏 引用 所属分类: C++ primer plus学习笔记

Feedback

# re: VC6.0中友元函数无法访问类私有成员的解决办法-------VC6.0的bug 2010-09-19 22:36 吴冬亮
sp6补丁装了就ok了

地址:
http://download.microsoft.com/download/1/9/f/19fe4660-5792-4683-99e0-8d48c22eed74/Vs6sp6.exe

就算认为自己装过了 也请重装遍 肯定行!  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理