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;
};
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;
};
晕`今天不经意看到一个关于你这程序的例子
才知道错在那了,可能你已经搞定了
#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);
}