标题:求教:友元函数无法访问类的私有成员
取消只看楼主
liwentao8181
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-2-4
结帖率:100%
已结贴  问题点数:20 回复次数:4 
求教:友元函数无法访问类的私有成员
#include <iostream>
using namespace std;
class CVector
{   
    private:
        int x;
        int y;
        int z;
    public:
        friend ostream& operator <<(ostream& out,CVector V);
};
ostream& operator <<(ostream& out,CVector V)
{
    out<<V.x<<endl;//编译时提示这里不能访问CVector的私有成员x。求教
    return out;
}
int main()
{
    return 0;
}
搜索更多相关主题的帖子: class 函数 private include public 
2012-02-04 17:00
liwentao8181
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-2-4
得分:0 
这也编译时出错。大虾们帮一下。
2012-02-04 17:01
liwentao8181
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-2-4
得分:0 
哦。版主好。我用的是vc++6.0提示错误。
2012-02-05 09:48
liwentao8181
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-2-4
得分:0 
回复 3楼 lz1091914999
版主好。我用的是vc++6。0编译的。
Deleting intermediate files and output files for project 'asd - Win32 Debug'.
--------------------Configuration: asd - Win32 Debug--------------------
Compiling...
main.cpp
f:\c++程序\asd\main.cpp(14) : error C2248: 'x' : cannot access private member declared in class 'CVector'
        f:\c++程序\asd\main.cpp(6) : see declaration of 'x'
执行 cl.exe 时出错.

asd.exe - 1 error(s), 0 warning(s)
2012-02-05 09:50
liwentao8181
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-2-4
得分:0 
多谢版主。
2012-02-05 19:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-360394-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.323983 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved