簡體   English   中英

重載運算符時出錯*

[英]Error in overloading operator *

我對運算符的重載有一點問題。 我有一個名為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 ,運算符才會AtmosphericData * qreal ,而當類型的順序相反時(在3 * (y0 - y1)表達式中會發生這種情況),運算符不會AtmosphericData * qreal

您還必須編寫一個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;
}

順便說一句,恕我直言,要實現數學運算符,您應該遵循通常的實現方式:首先實現分配版本( *= ),然后從“常規”( * )版本中調用它們; 有關更多詳細信息,請參見運算符重載常見問題解答

如果您寫:

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 否則,非會員非朋友應該是您的首選。

您正在嘗試將AtmostphereicData乘以一個int( 3*blah2*blah ),您似乎尚未定義該操作。

D定義一個運算符,該運算符使用int的左側和AtmosphereicData的右側。

您已經定義了一個operator *其第一個參數是AtmosphericData,第二個參數是數字。 但反之亦然。

您還應該定義其他運算符(作為非成員)

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

如果在類中聲明運算符,則只有在使用左側的類時,該運算符才有效。 要在右側使用您的類,您還需要將運算符重載為自由函數:

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

您還可以使用Boost.Operators並通過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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM