标题:一个小问题
只看楼主
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



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




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

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