#include<iostream>
using namespace std;
class Boat
{
public:
Boat(int w=0) {weight=w;}
void coutB() {cout<<"Boat weight="<<weight<<endl;}
friend void totalWeight(); //定义totalweight为boat的友元函数
private:
int weight;
};
class Car
{
Car(int w=0) {weight=w;}
void coutB() {cout<<"Car weight="<<weight<<endl;}
friend void totalWeight(); //定义totalweight为car的友元函数
private:
int weight;
};
void totalWeight()
{
int B,C;
B=boat.weight;
C=car.weight; //通过对象访问其weight
cout<<"total weight="<<B+C<<endl;
}
void main()
{
Boat boat(120);
Car car(110); //生成对象
totalWeight();
}
实现功能:totalweight计算两者的重量之和
编译报错:error C2065: 'boat' : undeclared identifier
error C2228: left of '.weight' must have class/struct/union type
error C2065: 'car' : undeclared identifier
error C2228: left of '.weight' must have class/struct/union type
error C2248: 'Car::Car' : cannot access private member declared in class 'Car'
see declaration of 'Car::Car'
请高手帮忙!!1
谢谢先~