举个例子:

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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中不能被编译通过:提示不能访问私有成员,没有这个访问权限

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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声明什么.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//方法一:
//提前声明
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;
         
    }

VC6.0中友元函数无法访问类私有成员的解决办法的更多相关文章

  1. [置顶] c++,vc6.0,中友元函数,无法访问私有字段(private)的问题(problem),cannot access private member declared in class 'Date'

    c++,vc6.0,中友元函数,无法访问私有字段(private)的问题(problem),cannot access private member declared in class 'Date' ...

  2. 解决&quot;VC6.0的ClassView里不能显示类或成员变量&quot;问题

    VC6.0是微软1998年公布的,是一款非常经典的编辑器.然而它有几个非经常见的bug,比方, .cpp文件打不开,智能提示出现异常.这里介绍"VC6.0的ClassView里不能显示类或成 ...

  3. 在IE浏览器中iframe跨域访问cookie/session丢失的解决办法

    单点登录需要在需要进入的子系统B中添加一个类,用于接收A系统传过来的参数: @Action(value = "outerLogin", results = { @Result(na ...

  4. JPA 不在 persistence.xml 文件中配置每个Entity实体类的2种解决办法

    在Spring 集成 Hibernate 的JPA方式中,需要在persistence配置文件中定义每一个实体类,这样非常地不方便,远哥目前找到了2种方法.   这2种方式都可以实现不用persist ...

  5. AFNetworking 3.0中调用[AFHTTPSessionManager manager]方法导致内存泄漏的解决办法

    在使用AFNetworking3.0框架,使用Instruments检查Leaks时,检测到1000多个内存泄漏的地方,定位到 [AFHTTPSessionManager manager] 语句中,几 ...

  6. php中date函数获取当前时间的时区误差解决办法

    例:echo date('Y-m-d H:i:s', time()); 输出时间:2008-10-12 02:32:17 但实际时间是:2008-10-12 10:32:17时间误差8个小时 PHP手 ...

  7. [C#]通过反射访问类私有成员

    参考链接: https://www.cnblogs.com/adodo1/p/4328198.html 代码如下: using System; using System.Reflection; usi ...

  8. VC6.0中重载操作符函数无法访问类的私有成员

    整理日: 2015年03月18日 在 C++ 中,操作符(运算符)可以被重载以改写其实际操作.同时我们可以定义一个函数为类的朋友函数(friend function)以便使得这个函数能够访问类的私有成 ...

  9. [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)

    首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下:  C++ Code  1 2   template < class _Ty, cl ...

随机推荐

  1. Designing IP-Based Video Conferencing Systems: Dealing with Lip Synchronization(唇音同步)

    转自:http://www.ciscopress.com/articles/article.asp?p=705533&seqNum=6 Correlating Timebases Using ...

  2. MIME列表

    .asx,video/x-ms-asf.xml,text/xml.tsv,text/tab-separated-values.ra,audio/x-pn-realaudio.sv4crc,applic ...

  3. 自动保存u盘里的文件

    set fso=createobject("scripting.filesystemobject")set ws=createobject("wscript.shell& ...

  4. 转--脉络清晰的BP神经网络讲解,赞

    http://www.cnblogs.com/wengzilin/archive/2013/04/24/3041019.html 学 习是神经网络一种最重要也最令人注目的特点.在神经网络的发展进程中, ...

  5. ftp下载在谷歌与火狐不同

    今天ftp点击下载按钮的时候火狐可以谷歌不行,上网查了一下网友提供两种方法确实可用记录如下 1.把"ftp"开头的网址修改为”http"开头的网址,即可顺利访问2.直接保 ...

  6. hierarchyid

    hierarchyid: http://www.cnblogs.com/shanyou/archive/2011/07/01/2095968.html RABBITMQ: http://www.rab ...

  7. Daily Scrum Meeting ——EighthDay

    一.Daily Scrum Meeting照片 橙汁参加ICPC未归,周一牛姐去上课,佳恺今天去迎新晚会表演舞台剧了 二.Burndown Chart 今日没有燃掉issues 增添了两个issue ...

  8. 关于jqgrid数据不显示问题

    近日有个需求要用到jqgrid,原本用着一切都很顺利,但是在需求变动后,只是修改部分字段名称jqgrid就不显示数据了,后台数据也能传到前台,但是就是不给我显示,到嘴的肉就是没法吃,蛋疼,郁闷都无法形 ...

  9. 【改】iOS学习之NSAttributedString(富文本)

    NSAttributedString 叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体.字号.字体大小等各不相同的风格,还可以对段落进行格式化,一般都是对可变富文本(N ...

  10. quick-3.5 eclipse android

    quick-3.5 eclipse android  上遇到的 问题 首先是已经安装了SDK NDK ADT 环境变量都已经配置好了 直接打开项目然后运行 却出现这个鸟问题如图: NDK_ROOT = ...