C++ 重载运算符时出错*
我对操作符的重载有点问题。我有一个名为AtmospheridData的类,我在其中定义了操作符* 在标题中,我在类中定义了此方法:C++ 重载运算符时出错*,c++,operator-overloading,C++,Operator Overloading,我对操作符的重载有点问题。我有一个名为AtmospheridData的类,我在其中定义了操作符* 在标题中,我在类中定义了此方法: //! Operator * (scalar) AtmosphericData operator*(const qreal& qrMult) const; .cpp文件中的定义如下: AtmosphericData AtmosphericData::operator*(const qreal& qrMult) const { Atmosph
//! Operator * (scalar)
AtmosphericData operator*(const qreal& qrMult) const;
.cpp文件中的定义如下:
AtmosphericData AtmosphericData::operator*(const qreal& qrMult) const
{
AtmosphericData xResult;
xResult.m_qrTemperature = this->m_qrTemperature * qrMult;
xResult.m_qrPressure = this->m_qrPressure * qrMult;
xResult.m_qrDensity = this->m_qrDensity * qrMult;
xResult.m_qrAbsoluteHumidity = this->m_qrAbsoluteHumidity * qrMult;
xResult.m_qrVisibility = this->m_qrVisibility * qrMult;
xResult.m_qrPrecipitationIndex = this->m_qrPrecipitationIndex * qrMult;
xResult.m_xWind.qrNS = this->m_xWind.qrNS * qrMult;
xResult.m_xWind.qrEW = this->m_xWind.qrEW * qrMult;
xResult.m_xWind.qrVert = this->m_xWind.qrVert * qrMult;
xResult.m_xPrecipitationType = this->m_xPrecipitationType;
return xResult;
}
然后,我在以下表达式中使用该类:
AtmosphericData c2;
AtmosphericData t1;
AtmosphericData t2;
AtmosphericData y0;
AtmosphericData y1;
qreal hx;
/* other code */
c2 = - (3 * (y0 - y1) + (hx * ((2 * t1) + t2))) / (hx * hx);
当我编译(在linux下使用qmakegcc)时,我得到以下错误
error: no match for ‘operator*’ in ‘3 * AtmosphericData::operator-(const AtmosphericData&) const(((const AtmosphericData&)((const AtmosphericData*)(& y1))))’
我似乎对运算符*声明做了一些错误,但我不明白我做错了什么
有人能告诉我如何纠正这个错误吗
感谢您的回复。如果您写:
c2 = - ((y0 - y1) * 3 + (hx * ((t1 * 2 ) + t2))) / (hx * hx);
而不是
c2 = - (3 * (y0 - y1) + (hx * ((2 * t1) + t2))) / (hx * hx);
那么它应该会起作用。因为代码中的运算符*
的定义要求大气数据
类型的对象应位于*
的左侧。也就是说,3*y
不起作用,但是当y
是类型为AtmosphericData
的对象时,y*3
起作用
如果希望3*y
也能工作,则将非成员函数定义为:
AtmosphericData operator*(const qreal &r, const AtmosphericData& a)
{
return d*r;//call the other overloaded, or calculate the value here if you wish!
}
我建议您将其他函数也定义为非成员函数。如果需要访问私有或受保护的成员,请将其设置为好友。否则,非会员非朋友应该是您的首选。如果您写:
c2 = - ((y0 - y1) * 3 + (hx * ((t1 * 2 ) + t2))) / (hx * hx);
而不是
c2 = - (3 * (y0 - y1) + (hx * ((2 * t1) + t2))) / (hx * hx);
那么它应该会起作用。因为代码中的运算符*
的定义要求大气数据
类型的对象应位于*
的左侧。也就是说,3*y
不起作用,但是当y
是类型为AtmosphericData
的对象时,y*3
起作用
如果希望3*y
也能工作,则将非成员函数定义为:
AtmosphericData operator*(const qreal &r, const AtmosphericData& a)
{
return d*r;//call the other overloaded, or calculate the value here if you wish!
}
我建议您将其他函数也定义为非成员函数。如果需要访问私有或受保护的成员,请将其设置为好友。否则,非成员非朋友应该是你的首选。C++中的算术运算符不是自动交换的,因此只有当你做了<代码> NoaleReDATA*QReals时,你的操作符才开始使用,但是当类型的顺序相反时(这就是在<代码> 3(Y0-Y1)表达式中发生的事情),你的操作符就不会出现。 您还必须编写一个
运算符*
来处理qreal*AtmosphericData
情况,该情况必须作为自由函数写入,因为左侧操作数的类型与您的类不同
inline AtmosphericData operator*(const qreal& lhs, const AtmosphericData & rhs)
{
// Just forward to the other operator (this works because I swapped the operands)
return rhs * lhs;
}
顺便说一下,要实现数学运算符,您应该遵循通常的模式,首先实现赋值版本(
*=
),然后从“正常”(*
)版本调用它们;请参阅详细信息。 < P>算术运算符在C++中不是自动交换的,因此,只有当你做了<代码> NoaleReDATA*QReals>代码>时,你的操作符才开始使用,但当类型的顺序相反时(这是在<代码> 3(Y0-Y1)表达式中发生的事情),你的操作符不会出现。
您还必须编写一个运算符*
来处理qreal*AtmosphericData
情况,该情况必须作为自由函数写入,因为左侧操作数的类型与您的类不同
inline AtmosphericData operator*(const qreal& lhs, const AtmosphericData & rhs)
{
// Just forward to the other operator (this works because I swapped the operands)
return rhs * lhs;
}
顺便说一下,要实现数学运算符,您应该遵循通常的模式,首先实现赋值版本(
*=
),然后从“正常”(*
)版本调用它们;有关更多详细信息,请参见。您正在尝试将AtmostphereicData乘以int(3*blah
和2*blah
),这是一种您似乎尚未定义的操作
d定义一个运算符,该运算符采用int的左侧和AtmosphereicData的右侧。您试图将AtmosphereicData乘以int(
3*blah
和2*blah
),这是一个您似乎尚未定义的操作
d定义一个运算符,该运算符采用int的左侧和AtmosphereicData的右侧。您定义了一个
运算符*
,其第一个参数是AtmosphereicData,第二个参数是数字。但反之亦然
您还应该定义另一个操作符(作为非成员)
您已经定义了一个
运算符*
,其第一个参数是AtmosphericData,第二个参数是数字。但反之亦然
您还应该定义另一个操作符(作为非成员)
如果在类内声明运算符,则只有在使用左侧的类时,它才会起作用。要使用右侧的类,还需要将操作符重载为自由函数:
AtmosphericData operator*(const qreal& qrMult, const AtmosphericData& data);
您还可以使用operator*=
免费获取所有这些
class AtmosphericData : boost::multipliable<AtmosphericData, qreal>
{
// blah
public:
AtmosphericData& operator*=(const qreal& qrMult) {
// blah blah
return *this;
}
// by inheriting from boost::multipliable, this gives you both operator* for free
}
class AtmosphericData:boost::可倍增
{
//废话
公众:
大气数据和操作员*=(常数qreal和qrMult){
//废话
归还*这个;
}
//通过继承boost::Multiplibable,这将免费为您提供两个运算符*
}
如果在类中声明运算符,则只有在左侧使用类时,它才会起作用。要使用右侧的类,还需要将操作符重载为自由函数:
AtmosphericData operator*(const qreal& qrMult, const AtmosphericData& data);
您还可以使用operator*=
免费获取所有这些
class AtmosphericData : boost::multipliable<AtmosphericData, qreal>
{
// blah
public:
AtmosphericData& operator*=(const qreal& qrMult) {
// blah blah
return *this;
}
// by inheriting from boost::multipliable, this gives you both operator* for free
}
class AtmosphericData:boost::可倍增
{
//废话
公众:
大气数据和操作员*=(常数qreal和qrMult){
//废话
归还*这个;
}
//通过继承boost::Multiplibable,这将免费为您提供两个运算符*
}
既然您要在类定义中声明运算符,您确定要将其公开吗?是的,但现在我在qreal参数和atmosphericdata参数之间添加了带交换顺序的乘法覆盖,它似乎起作用了。既然您要在类定义中声明运算符,您确定要