重载操作符*出错

Error in overloading operator *

本文关键字:出错 操作符 重载      更新时间:2023-10-16

我有一个操作符重载的小问题。我有一个名为AtmospheridData的类,我在其中定义了操作符*.

在头文件中,我在类中定义了这个方法:
//! 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下使用qmake-gcc),我得到以下错误

error: no match for ‘operator*’ in ‘3 * AtmosphericData::operator-(const AtmosphericData&) const(((const AtmosphericData&)((const AtmosphericData*)(& y1))))’

我似乎对操作符*声明做了一些错误,但是我不明白我做错了什么。

谁能告诉我怎样才能纠正这个错误?

感谢您的回复。

c++中的算术运算符不是自动交换的,因此只有当您执行AtmosphericData * qreal时,操作符才会生效,而当类型的顺序相反时(这就是3 * (y0 - y1)表达式中发生的情况),操作符不会生效。

你必须写一个operator*来处理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;
}

顺便说一下,要实现数学运算符,你应该遵循通常的模式,首先实现赋值版本(*=),然后从"正常"版本(*)调用它们;

您正在尝试将atmostphericdata乘以int (3*blah2*blah),您似乎没有定义的操作。

定义一个操作符,取int的左边和AtmosphereicData的右边

您已经定义了一个operator *,它的第一个参数是AtmosphericData,第二个参数是一个数字。但不是相反。

还应该定义另一个操作符(作为非成员)

AtmosphericData operator * (qreal num, const AtmosphericData& ad )
{
    return ad*num;
}

如果你写:

c2 = - ((y0 - y1) * 3 + (hx * ((t1 * 2 ) + t2))) / (hx * hx);

代替

c2 = - (3 * (y0 - y1) + (hx * ((2 * t1) + t2))) / (hx * hx);

那么它应该可以工作。因为代码中operator*的定义要求AtmosphericData类型的对象应该位于*的左侧。也就是说,3 * y不能工作,但y*3可以工作,yAtmosphericData类型的对象。

如果您希望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!
}

我建议您也将其他函数定义为非成员函数。如果需要访问私有或受保护的成员,则将其设置为friend。否则,非会员非好友应该是您的首选。

如果在类内部声明操作符,则只有在左侧使用类时才有效。要在右侧使用类,还需要将操作符重载为自由函数:

AtmosphericData operator*(const qreal& qrMult, const AtmosphericData& data);

你也可以使用Boost。操作符和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
}