注册 登录
编程论坛 C++教室

友元函数问题

bibingyan 发布于 2008-05-12 19:36, 1012 次点击
友元函数不能访问类的私有变量是怎么回事啊?    帮忙分析下原因
12 回复
#2
sunkaidong2008-05-12 19:36
可以
#3
sunkaidong2008-05-12 19:37
但是6.0对友员支持不好..你要用命名空间隔忾
#4
bibingyan2008-05-12 19:38
要全部贴出来吗?
#5
sunkaidong2008-05-12 19:39
恩..贴出来看看啊
#6
bibingyan2008-05-12 19:39
是用命名空间,我是初学者,还在试;
#7
bibingyan2008-05-12 19:42
头文件 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;
}
#8
bibingyan2008-05-12 19:45
我在线等着啊...谢谢你的帮忙呢.
#9
sunkaidong2008-05-12 19:49
#include<iostream>
using namespace std;
namespace a{
class demo
{
private:
     static int i;
     int j;
public:
    demo():j(0){};
    friend ostream& operator<<(ostream & ,demo &);
};
int demo::i=0;
ostream& operator<<(ostream & os,demo &a)
{
    os<<a.j<<endl;
    return os;
}
}
int main(void)
{
    a::demo d;
    cout<<d;
    return 0;
}
#10
sunkaidong2008-05-12 19:55
#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
#include<iostream>
#include"friends.h"
using namespace std;
//using namespace friends;
friends::Box::Box(double rlength,double rwidth,double rheight):length(rlength),width(rwidth),height(rheight){}
friends::Box::~Box()
{
    cout<<"析构"<<endl;
}
void friends::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 friends::BoxSurface(const Box& other1)
{
    return  other1.height+other1.width+other1.length;
}
#11
bibingyan2008-05-12 19:56
还是不晓得调啊.
#12
bibingyan2008-05-12 19:59
你调了那个程序没?  好像是说不能放问类的私有成员
#13
bibingyan2008-05-12 20:05
呵呵,出来了呢,谢谢你啊,原来是那个友元函数没有加空间名字的.
1