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

举个例子:


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;

        

    }

时间: 2024-08-06 11:32:48

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

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

VC6.0是微软1998年发布的,是一款很经典的编辑器,然而它有几个很常见的bug,比如, .cpp文件打不开,智能提示出现异常,这里介绍"VC6.0的ClassView里不能显示类或成员变量"问题的解决方法.详细步骤如下: 1) 关闭VC6.0,找到工程目录里的.clw文件,按Del键删除该 .clw文件,如图(1)所示: 图(1)按Del键删除.clw文件 2)打开VC6.0里的工程,按Ctrl+W –> OK,如图(2).图(3).图(4)所示: 图(2)点击"是

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

原文:JPA 不在 persistence.xml 文件中配置每个Entity实体类的2种解决办法 在Spring 集成 Hibernate 的JPA方式中,需要在persistence配置文件中定义每一个实体类,这样非常地不方便,远哥目前找到了2种方法. 这2种方式都可以实现不用persistence.xml文件,免去每个Entity都要在persistence.xml文件中配置的烦恼,但是这种方式Entity实体类的主键字段注解@ID要放到 getXXX()方法上,否则不认. 方式1: 修改

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

参考链接: https://www.cnblogs.com/adodo1/p/4328198.html 代码如下: 1 using System; 2 using System.Reflection; 3 using UnityEngine; 4 5 public static class ObjectExtension 6 { 7 //获取私有字段的值 8 public static T GetPrivateField<T>(this object instance, string fiel

通过Java反射测试类私有成员

在Java开发阶段,因为追求架构规范和遵循设计原则,所以要用private和protected修饰符去定义类的成员方法.变量.常量,这使得代码具封装性.内聚性等,但在测试阶段会造成一定的不便.通过Java的反射机制,便能很好地解决该问题. ReflectUtil.java //...... /** * @author yumin * @since 2015-03-02 14:52 */ public class ReflectUtil { private ReflectUtil() { } //

(转)VC6.0中OpenGL开发环境配置

首先简单介绍一下OpenGL: OpenGL作为当前主流的图形API之一,它在一些场合具有比DirectX更优越的特性.       OpenGL官方网站(英文)    http://www.opengl.org 然后设置编程的一些环境,及其安装必备文件的步骤如下: 第一步:选择一个编译环境 现在Windows系统的主流编译环境有Visual Studio,Broland C++ Builder,Dev-C++等,它们都是支持OpenGL的.但这里我们选择VC++ 6.0作为学习OpenGL的环

在VC6.0中能不能使用Duilib界面库呢?

Duilib库的源代码是在vs2010下编译的,一般适用于vs2008及以上的版本开发使用,那么duilib能不能在vc6.0的工程中使用呢?如何在vc6.0中使用duilib库呢? 今天,由于工作要求,需要在vc6.0下使用duilib库,百度了很久,未果,所以自己来尝试了一下! 1.在vc6.0中,新建一个MFC对话框工程,我用的是mfc对话框,习惯这么用. 2.将Duilib头文件及lib引用库拷贝到工程目录,引用进去. 3.新建duidlg类,这个类是自己定义的,对应这duilib里的窗

[转]高速掌握VC6.0中各种宏注释(附图)

为了方便别人或自己阅读自己的程序,注释是坚决不可少的.一个漂亮的程序,不是在于你应用的技术多么高深,而是能够把高深的技术描述的清楚易懂. 在Java的IDE环境——Eclispe中,有很多中注释的,并且设置注释也是很方便的,因为现在从事C++,嘻嘻,Eclispe已经卸载,至于设置注释的地方,直接百度或谷歌即可. 所以嘛,习惯了Eclispe的注释,所以想法设法,在VC6.0中尝试.当对于一个陌生的东西而言,如何熟悉他呢,就是拿你现在已有的知识,去联想.比如Java中截取字符串,或解析xml等,

批量删除VC6.0中的临时文件

在VC6.0中,选择工具栏上的[Build]–> Clean,可以直接删除Debug目录里的临时文件.这种方法只能删除单个工程里的临时文件,如果要删除多个工程里的临时文件,则需要使用批处理命令来进行批量文件删除.代码如下: del *.obj *.pch *.sbr *.pdb *.idb *.ilk *.ncb *.opt *.bsc *.res *.exp *.lib *.aps/s 1)将上述代码保存在clearVC.txt中,再将clearVC.txt另存为 clearVC.bat.如图

VC6.0中的灰色字体是什么?

VC6.0中: 绿色字体是注释, 蓝色字体是关键字, 黑色字体是用户写的程序, 灰色字体是向导自动产生的代码.