【求助】vc++菜鸟作业题求助~
目的是使用友元函数的方法求船和车重量的和
#include "iostream.h"
class Boat;
class Car{
friend int totalWeight(Boat &a,Car &b);
public:
int GetNumber()
{
cin >> Number;
return Number;
}
int GetWeight()
{
cin >> Weight;
return Weight;
}
private:
int Number,Weight;
};
class Boat{
friend int totalWeight(Boat &a,Car &b);
public:
int GetNumber()
{
cin >> Number;
return Number;
}
int GetWeight()
{
cin >> Weight;
return Weight;
}
private:
int Number,Weight;
};
int totalweight(Boat& a,Car& b)
{
return a.Weight+b.Weight;
}
int main()
{
Boat a;
Car b;
int x,y,z;
x = a.GetWeight();
y = b.GetWeight();
z = totalweight(a,b);
cout << z << endl;
return 0;
}
结果显示错误如下:
--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.cpp
F:\New Folder (3)\5141\1.cpp(41) : error C2248: 'Weight' : cannot access private member declared in class 'Boat'
F:\New Folder (3)\5141\1.cpp(36) : see declaration of 'Weight'
F:\New Folder (3)\5141\1.cpp(41) : error C2248: 'Weight' : cannot access private member declared in class 'Car'
F:\New Folder (3)\5141\1.cpp(19) : see declaration of 'Weight'
Error executing cl.exe.
1.exe - 2 error(s), 0 warning(s)