求救啊!!!!C++高手来啊!关于友元函数题。。

题目:用C++定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数totalWeight(),计算二者的重量和
我的见解:网上很多人解答出来但他们把weight定义在public中我觉得没意义,友元函数是访问私有变量的。。
我的代码:#include"iostream"
using namespace std;
class Car;
class Boat
{
private:
double weight;
friend double totalweight(Boat&boat,Car&car);
public:
double goin1(double a)
{
weight=a;
return 1;
}

};
class Car
{
private:
double weight;
friend double totalweight(Boat&boat,Car&car);
public:
double goin2(double b)
{
weight=b;
return 1;
}

};
double totalweight(Boat&boat,Car&car)
{
return( Boat::weight)+(Car::weight);
}

int main()
{
double a,b,c;
cout<<"请输入Car,Boat重量"<<endl;
cin>>a>>b;
Boat A;
Car B;
A.goin1(a);
B.goin2(b);
cout<<"总重量为:"<<totalWeight(boat,car)<<endl
}

这是我写的麻烦各位大大帮我看一下。。。
编译出错:1.(VC2008): error C2597: 对非静态成员“Boat::weight”的非法引用
1>e:\c++\盒子类\盒子类\实现2.cpp(32) : error C3867: “Boat::weight”: 函数调用缺少参数列表;请使用“&Boat::weight”创建指向成员的指针
1>e:\c++\盒子类\盒子类\实现2.cpp(32) : error C2597: 对非静态成员“Car::weight”的非法引用
1>e:\c++\盒子类\盒子类\实现2.cpp(32) : error C3867: “Car::weight”: 函数调用缺少参数列表;请使用“&Car::weight”创建指向成员的指针
1>e:\c++\盒子类\盒子类\实现2.cpp(32) : error C2568: “+”: 无法解析函数重载
1> 无法从以前的错误中恢复;正在停止编译
2.(VC++6.0):ar and boat.cpp
c:\c++\car and boat.cpp(32) : error C2597: illegal reference to data member 'Boat::weight' in a static member function
c:\c++\car and boat.cpp(32) : error C2597: illegal reference to data member 'Car::weight' in a static member function
c:\c++\car and boat.cpp(32) : error C2568: '+' : unable to resolve function overload
c:\c++\car and boat.cpp(44) : error C2065: 'totalWeight' : undeclared identifier
c:\c++\car and boat.cpp(44) : error C2065: 'boat' : undeclared identifier
c:\c++\car and boat.cpp(44) : error C2065: 'car' : undeclared identifier
c:\c++\car and boat.cpp(45) : error C2143: syntax error : missing ';' before '}'
c:\c++\car and boat.cpp(45) : warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错.

car and boat.obj - 1 error(s), 0 warning(s)
我苦想了几个小时未果 临近崩溃请教各位大大贡献我所有积分33

呵呵都是小问题楼主要细心呦~注意看注释
class Car;
class Boat
{
private:
double weight;
friend double totalweight(Boat&boat,Car&car);
public:
double goin1(double a)
{
weight=a;
return 1;
}

};
class Car
{
private:
double weight;
friend double totalweight(Boat&boat,Car&car);
public:
double goin2(double b)
{
weight=b;
return 1;
}

};
double totalweight(Boat &boat,Car &car)
{
return( boat.weight)+(car.weight);//注意这里用对象调用私有数据成员不是类!
}

int main()
{
double a,b,c;
cout<<"请输入Car,Boat重量"<<endl;
cin>>a>>b;
Boat A;
Car B;
A.goin1(a);
B.goin2(b);
cout<<"总重量为:"<<totalweight(A,B)<<endl;//注意这行定义totalweight中W应是小写,且楼主最后丢掉了 ; totalweight参数是应是Boat 与Car的对象
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-05-25
这问题你想了几个小时?我还能说什么呢
double totalweight(Boat&boat,Car&car)
{
return( boat.weight)+(car.weight);
}

int main()
{
double a,b,c;
cout<<"请输入Car,Boat重量"<<endl;
cin>>a>>b;
Boat A;
Car B;
A.goin1(a);
B.goin2(b);
cout<<"总重量为:"<<totalweight(A,B)<<endl;
}
第2个回答  2010-05-25
修改完毕!

#include"iostream"
using namespace std;
class Car;
class Boat
{
private:
double weight;
friend double totalweight(Boat&boat,Car&car);
public:
double goin1(double a)
{
weight=a;
return 1;
}

};
class Car
{
private:
double weight;
friend double totalweight(Boat&boat,Car&car);
public:
double goin2(double b)
{
weight=b;
return 1;
}

};
double totalweight(Boat&boat,Car&car)
{
return( boat.weight)+(car.weight);
}

int main()
{
double a,b;
cout<<"请输入Car,Boat重量"<<endl;
cin>>a>>b;
Boat boat;
Car car;
boat.goin1(a);
car.goin2(b);
cout<<"总重量为:"<<totalweight(boat,car)<<endl;
return(0);
}
第3个回答  2010-05-25
#include <iostream>
using namespace std;
class Car;
class Boat
{
private:
double weight;
friend double totalweight(Boat&boat,Car&car);
public:
double goin1(double a)
{
weight=a;
return 1;
}

};
class Car
{
private:
double weight;
friend double totalweight(Boat&boat,Car&car);
public:
double goin2(double b)
{
weight=b;
return 1;
}

};
double totalweight(Boat&boat,Car&car)
{
return( boat.weight)+(car.weight);
}

int main()
{
double a,b,c;
cout<<"请输入Car,Boat重量"<<endl;
cin>>a>>b;
Boat A;
Car B;
A.goin1(a);
B.goin2(b);
cout<<"总重量为:"<<totalWeight(A,B)<<endl;
return 0;
}
第4个回答  2010-05-25
#include <iostream>
using namespace std;

class Car;
class Boat
{
friend double totalweight( Boat &boat,Car &car);
private:
double weight;

public:
double goin1(double a)
{
weight=a;
return 1;
}

};

class Car
{
friend double totalweight(Boat &boat,Car &car);
private:
double weight;

public:
double goin2(double b)
{
weight=b;
return 1;
}

};

double totalweight(Boat&boat,Car&car)
{
return boat.weight + car.weight ;
};

int main()
{
double a,b;
cout<<"请输入Car,Boat重量"<<endl;
cin>>a>>b;
Boat A;
Car B;
A.goin1(a);
B.goin2(b);
cout<<"总重量为:"<<totalweight( A, B )<<endl;
}
第5个回答  2010-05-25
#include<iostream>
using namespace std;
class Car;
class Boat
{
private:
double weight;
friend double totalweight(Boat &boat,Car &car);
public:
double goin1(double a)
{
weight=a;
return 1;
}

};
class Car
{
private:
double weight;
friend double totalweight(Boat &boat,Car &car);
public:
double goin2(double b)
{
weight=b;
return 1;
}

};
double totalweight(Boat &boat,Car &car)
{
return(boat.weight)+(car.weight);
}

int main()
{
double a,b;
cout<<"请输入Car,Boat重量"<<endl;
cin>>a>>b;
Boat A;
Car B;
A.goin1(a);
B.goin2(b);
cout<<"总重量为:"<<totalweight(A,B)<<endl;
return 0;
}

两个错误:
double totalweight(Boat&boat,Car&car)
{
return( Boat::weight)+(Car::weight);
}
改为:
double totalweight(Boat &boat,Car &car)
{
return(boat.weight)+(car.weight);
}

这个:
cout<<"总重量为:"<<totalWeight(boat,car)<<endl
少个分号,函数名写错了。
改为:
cout<<"总重量为:"<<totalweight(A,B)<<endl;
相似回答