友元函数 无法访问 类私有成员
来源:互联网 发布:弓箭女皇升级数据 编辑:程序博客网 时间:2024/06/05 22:46
#include<iostream>
02
using
namespace
std;
03
class
cylinder
04
{
05
friend
istream operator>>(istream& is,cylinder &cy);
06
public
:
07
inline
double
square()
08
{
return
length*(width+height)*2+width*height*2; }
09
inline
double
volume()
10
{
return
length*width*height; }
11
private
:
12
double
length;
13
double
width;
14
double
height;
15
};
16
istream operator>>(istream is,cylinder &cy)
17
{
18
cout<<
"input length:"
<<endl;
19
is>>cy.length;
20
cout<<
"input width:"
<<endl;
21
is>>cy.width;
22
cout<<
"input height:"
<<endl;
23
is>>cy.height;
24
return
is;
25
}
26
int
main()
27
{
28
cylinder first;
29
cin>>first;
30
cout<<first.square()<<endl;
31
cout<<first.volume()<<endl;
32
return
0;
33
}
这些代码在VC6.0中不能被编译通过:提示不能访问私有成员,没有这个访问权限
改成这样就可以了,代码如下:
01
#include<iostream>
02
using
std::cin;
03
using
std::endl;
using
std::cout;
04
using
std::ostream;
05
using
std::istream;
06
using
std::ostream;
07
class
cylinder
08
{
09
friend
istream operator>>(istream& is,cylinder &cy);
10
public
:
11
inline
double
square()
12
{
return
length*(width+height)*2+width*height*2; }
13
inline
double
volume()
14
{
return
length*width*height; }
15
private
:
16
double
length;
17
double
width;
18
double
height;
19
};
20
istream operator>>(istream is,cylinder &cy)
21
{
22
cout<<
"input length:"
<<endl;
23
is>>cy.length;
24
cout<<
"input width:"
<<endl;
25
is>>cy.width;
26
cout<<
"input height:"
<<endl;
27
is>>cy.height;
28
return
is;
29
}
30
int
main()
31
{
32
cylinder first;
33
cin>>first;
34
cout<<first.square()<<endl;
35
cout<<first.volume()<<endl;
36
return
0;
37
}
原因:
这据说是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声明什么.
下面给出流浪给的解决办法:
01
//方法一:
02
//提前声明
03
class
cylinder;
04
istream &operator>>(istream& is,cylinder &cy);
05
06
//方法二:
07
//不用命名空间 或者 像晨雨那样写
08
#include<iostream.h>
09
10
//方法三:
11
12
class
cylinder
13
{
14
friend
istream &operator>>(istream& is,cylinder &cy)
//写在类里面
15
{
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
24
}
25
..........
26
27
//方法四:打SP6补丁,貌似不好使。。。(呵呵,是貌似也没用)
28
29
//方法五:换别的对标准C++支持好的编译器,如DEV C++/。。。(呵呵)
- 友元函数 无法访问 类私有成员
- 友元函数 无法访问 类私有成员
- VS2015友元函数重载操作运算符无法访问私有成员
- C++友元函数访问类的私有成员
- C++友元函数访问私有成员
- 友元函数、友元类、访问私有成员
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- 不使用成员函数或者友元函数访问类的私有成员变量
- 使用友元函数实现用外部函数修改类内的私有数据成员
- C++访问类的私有成员变量的方法(友元函数、友元类)
- “friend声明友元函数,友元函数却依旧无法访问该类的私有属性”的解决方法
- Android 调试.so库文件
- 如何分析 kernel panic
- Android权限不够问题
- 函数模板中使用类型萃取(traits)替换类型推导(deduce)
- 友元函数 无法访问 类私有成员
- 友元函数 无法访问 类私有成员
- 今天努力吧!
- Proxy (C++实现)
- 杭电 hdu 2000 ASCII码排序
- ubuntu10.04 wine1.3 安装 IE6、QQ和迅雷
- 企业如何架设代理服务器联接互联网
- Java内存机制详解
- 分区举例
- 使用VC6.0实现窗口的任意分割