VC6.0中友元函数无法访问类私有成员的解决办法
来源:互联网 发布:网络词汇 编辑:程序博客网 时间:2024/05/17 20:27
举个例子:
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
#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;
}
..........
//方法四:打SP6补丁,貌似不好使。。。(呵呵,是貌似也没用)
//方法五:换别的对标准C++支持好的编译器,如DEV C++/。。。(呵呵)
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法
- VC6.0中友元函数无法访问类私有成员的解决办法-------VC6.0的bug
- VC6.0中友元函数无法访问类私有成员的解决办法-------VC6.0的bug
- VC6.0中友元函数无法访问类私有成员的解决办法(VC6.0的bug)
- VC6.0中友元函数无法访问类私有成员的解决办法-------VC6.0的bug
- VC6.0中重载操作符函数无法访问类的私有成员[转]
- VC6_0中友元函数无法访问类私有成员的解决办法---VC6_0的经典bug(转)
- 解决VC6.0中友元方式重载运算符时无法访问类私有成员
- VC6.0中友元函数访问类的私有成员的bug
- 友元函数 无法访问 类私有成员
- 友元函数 无法访问 类私有成员
- [JavaScript]AJAX方法简单示例——1(.load()方法)
- 黑马程序员-----java基础二十四(银行柜台取票管理方案代码实现)
- Huffman编码
- Tomcat Too Many Open Files
- 宏与内联函数
- VC6.0中友元函数无法访问类私有成员的解决办法
- JavaScript replace(rgExp,fn)正则替换的用法
- H.264 Profile、Level、Encoder三张简图
- android内存溢出问题(out of memory)
- jsp中实现批量删除
- HDU 1157 中位数有更高效的方法,编程之美又提到,以后实践
- 设计模式六大原则(2):里氏替换原则
- STL应用
- 字符串分割(C++)