| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1404 人关注过本帖
标题:(出错)友元函数的调用
只看楼主 加入收藏
hxjtiger
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2007-10-9
收藏
 问题点数:0 回复次数:3 
(出错)友元函数的调用
#include<iostream>
using namespace std;
class car;
class boat
{
public:
 
 boat(int nw=100)
 {
  w=nw;
 }
 friend int car::total(boat &p1,car &p2);
private:
 int w;
};
class car
{
public:
 
 car(int nw=50)
 {
  w=nw;
 }
    int total(boat &p1,car &p2)
 {
  return p1.w+p2.y ;
 }
private:
 int y;
};
void main()
{
 boat boat1(1000);
 car car1(2000);
 cout<<"totalweight is"<<car1.total(boat1,car1)<<endl;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
你们帮我看看,怎么是一个类中的函数成为另一个类的友元函数调用类中的私有数据????
搜索更多相关主题的帖子: car boat int 函数 
2007-11-28 20:45
quying257
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-11-29
收藏
得分:0 
刚刚调了一下
把声明friend int car::total(boat &p1,car &p2);
改为friend  car;
就好用了 可为什么上面的形式不行啊 到底错到哪了?
2007-11-29 13:58
无缘今生
Rank: 2
等 级:新手上路
威 望:3
帖 子:523
专家分:7
注 册:2007-6-25
收藏
得分:0 
friend int car::total(boat &p1,car &p2);
你搞清楚:你所用的total()是类car的友元,你写成car::total()就是说total()是car的成员函数,
前后不一致,当然不行啦。

时不再来!!!
2007-11-29 16:51
o0花生0o
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2007-10-13
收藏
得分:0 
你定义的友元函数是两个类的友元函数,必须要在两个类中声明,在类car中
 int  total(boat &p1,car &p2)之前要加上friend
在调用友元函数输出结果时,由于total是两个类的友元函数,所以输出语句应该为
cout<<"totalweight is"<<total(boat1,car1)<<endl;
2007-11-29 23:54
快速回复:(出错)友元函数的调用
数据加载中...
 
   



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

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