您的实施有多个问题。您会收到两种形状,但circleType
没有变体名称。然后我看到你试图调用没有变量的函数calcVolume
。你怎么能计算什么都没有?那么,你不能。您必须使用圆圈的名称并参考它。
// v---- Give a name to your circle
bool equalVolume(const CircleType& myCircle, const CylinderType& myCylinder) {
// v---- Call calcVolume with myCircle
if (myCircle.calcVolume() != myCylinder.calcVolume()) {
// call with the cylinder --^
return false
} else {
return true;
}
}
顺便说一句,因为相比已经是bool
类型的表达式,可以缩小你的函数是:
bool equalVolume(const CircleType& myCircle, const CylinderType& myCylinder) {
return myCircle.calcVolume() == myCylinder.calcVolume();
}
我宁愿创建一个返回音量的功能并将实现留给用户。它看起来像:
using volume_t = double;
struct Circle {
volume_t volume() const {
// return volume here
}
};
struct Cylinder {
volume_t volume() const {
// return volume here
}
};
然后,而不是使用isVolumeEqual
功能,只要做到这一点:
if (myCircle.volume() == myCylinder.volume()) {
// code here ...
}
如果你真的想要实现的容量相等的功能,我会做它像即:
template<typename S1, typename S2>
auto volumeEqual(const S1& s1, const S2& s2) -> decltype(s1.volume() == s2.volume()) {
return s1.volume() == s2.volume();
}
这样一来,你实施具有volume()
函数的所有可能的形状一volumeEqual
。
你做了一些错别字纠正他们,然后它会工作,如果'circelType'和'cylinderType'具有'calcVolume'函数 –
您可能意指'返回(cleft.CalcVolume()== cright.CalcVolume()); '作为函数的主体''bool equalVolume(const circleType&cleft,const cylinderType&cright)' –
您可能会重新设计您的应用程序,以使'class circleType'和'class cylinderType'继承自* common *超类'GeometricalObject',虚拟double CalcVolume(void)const;'方法。然后你只需要一个'bool sameValue(const GeometricalObject&left,GeometricalObject&right);'函数 –