标题:友元函数问题
取消只看楼主
bibingyan
Rank: 1
来 自:湖南长沙
等 级:新手上路
帖 子:123
专家分:0
注 册:2008-3-16
 问题点数:0 回复次数:7 
友元函数问题
友元函数不能访问类的私有变量是怎么回事啊?    帮忙分析下原因
搜索更多相关主题的帖子: 函数 
2008-05-12 19:36
bibingyan
Rank: 1
来 自:湖南长沙
等 级:新手上路
帖 子:123
专家分:0
注 册:2008-3-16
得分:0 
要全部贴出来吗?
2008-05-12 19:38
bibingyan
Rank: 1
来 自:湖南长沙
等 级:新手上路
帖 子:123
专家分:0
注 册:2008-3-16
得分:0 
是用命名空间,我是初学者,还在试;
2008-05-12 19:39
bibingyan
Rank: 1
来 自:湖南长沙
等 级:新手上路
帖 子:123
专家分:0
注 册:2008-3-16
得分:0 
头文件 friends.h
#ifndef FRIENDF
#define FRIENDF
namespace friends
{
    class Box
    {
    public:
        Box(double rlength=1.0,double rwidth=1.0,double rheight=1.0);
        ~Box();
        void Show();
        //friend double BoxSurface(const Box& other1,const Box& other2);
        friend double BoxSurface(const Box& other1);
    private:
        double length;
        double width;
        double height;
    };
}
#endif
定义文件:
friends.cpp
#include<iostream>
#include"friends.h"
using namespace std;
using namespace friends;
Box::Box(double rlength,double rwidth,double rheight):length(rlength),width(rwidth),height(rheight){}
Box::~Box()
{
    cout<<"析构"<<endl;
}
void Box::Show()
{
    cout<<"长:"<<length<<endl
        <<"宽:"<<width<<endl
        <<"高;"<<height<<endl;

}
主文件:
#include<iostream>
#include"friends.h"
using namespace std;
using namespace friends;

int main()
{
    Box aa(10.0,20.0,30.0);
    aa.Show();
    Box bb(40.0,50.0,60.0);
    bb.Show();
    cout<<"两高: "<<BoxSurface(aa)<<endl;
    return 0;
}
double BoxSurface(const Box& other1)
{
    return  other1.height+other1.width+other1.length;
}
2008-05-12 19:42
bibingyan
Rank: 1
来 自:湖南长沙
等 级:新手上路
帖 子:123
专家分:0
注 册:2008-3-16
得分:0 
我在线等着啊...谢谢你的帮忙呢.
2008-05-12 19:45
bibingyan
Rank: 1
来 自:湖南长沙
等 级:新手上路
帖 子:123
专家分:0
注 册:2008-3-16
得分:0 
还是不晓得调啊.
2008-05-12 19:56
bibingyan
Rank: 1
来 自:湖南长沙
等 级:新手上路
帖 子:123
专家分:0
注 册:2008-3-16
得分:0 
你调了那个程序没?  好像是说不能放问类的私有成员
2008-05-12 19:59
bibingyan
Rank: 1
来 自:湖南长沙
等 级:新手上路
帖 子:123
专家分:0
注 册:2008-3-16
得分:0 
呵呵,出来了呢,谢谢你啊,原来是那个友元函数没有加空间名字的.
2008-05-12 20:05



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




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

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