| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1089 人关注过本帖
标题:一个小问题
只看楼主 加入收藏
comebaby
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2006-6-17
收藏
得分:0 

class Car
{public://你这里是不是得加这个,不加的话,默认为私有
Car(int w=0) {weight=w;}
void coutB() {cout<<"Car weight="<<weight<<endl;}
friend void totalWeight(Boat &a,Car &b); //定义totalweight为car的友元函数
private:
int weight;
};

2006-10-05 21:30
comebaby
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2006-6-17
收藏
得分:0 

晕`今天不经意看到一个关于你这程序的例子
才知道错在那了,可能你已经搞定了

#include<iostream>
using namespace std;

class Car;

class Boat
{
public:
Boat(int w=0) {weight=w;}
void coutB() {cout<<"Boat weight="<<weight<<endl;}
friend void totalWeight(Boat &a,Car &b); //定义totalweight为boat的友元函数
private:
int weight;
};

class Car
{public:
Car(int w=0) {weight=w;}
void coutB() {cout<<"Car weight="<<weight<<endl;}
friend void totalWeight(Boat &a,Car &b); //定义totalweight为car的友元函数
private:
int weight;
};

void totalWeight(Boat &p1,Car &p2)
{
int x,y;
x=p1.weight;
y=p2.weight; //通过对象访问其weight
cout<<"total weight="<<x+y<<endl;
}

void main()
{
Boat boat(120);
Car car(110); //生成对象
totalWeight(boat,car);
}

2006-10-10 13:18
天使坠落的眼泪
Rank: 2
等 级:新手上路
威 望:5
帖 子:204
专家分:0
注 册:2006-5-16
收藏
得分:0 

樹欲靜而風不止... http://blog./seablue_xj
2006-10-11 09:26
快速回复:一个小问题
数据加载中...
 
   



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

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