c++ - Error in overloading operator * -


I have a small problem with overloading of an operator. I have a square of atmospheric data name, in which I define operator *.

In the heading I define this method in class:

  //! Operator * (Scalar) Atmospheric Data Operator * (Constant and QRMult) Const.   

And definition. In the CPP file, the following is:

  Atmospheric Atmospheric Data :: Operator * (const qreal & amp; qrMult) const {Atmospheric data xResult; XResult.m_qr temperature = it- & gt; M_qr temperature * QRMult; XResult.m_qrpressure = it- & gt; M_qrpressure * qrMult; XResult.m_qrDensity = it- & gt; M_qrDensity * qrMult; XResult.m_qrAbsoluteHumidity = this- & gt; M_crabsolutamality * qrMult; XResult.m_qrVisibility = it- & gt; M_qrVisibility * qrMult; XResult.m_qrPrecipitationIndex = it- & gt; M_qrPrecipitationIndex * qrMult; XResult.m_xWind.qrNS = this- & gt; M_xWind.qrNS * qrMult; XResult.m_xWind.qrEW = THIS- & gt; M_xWind.qrEW * qrMult; XResult.m_xWind.qrVert = it- & gt; M_xWind.qrVert * qrMult; XResult.m_xPrecipitationType = this-> M_xPrecipitationType; Return xResult; }   

Then, I use the class in the following expression:

  Atmospheric data c 2; Atmospheric data T1; Atmospheric data T2; Atmospheric data y0; Atmospheric data y1; Qreal hx; / * Other Code * / C2 = - (3 * (y0 - y1) + (HX * ((2 * T1 + + T2))) / (Hx * HX);   

When I compile (using qmake-gcc under linux), I get the following error

  error: 'operator * There is no match for '???? At atmospheric data :: operator- (const atmospheric data) const (const atmospheric data) ((at constant atmospheric data *) (and y1))) in one   

I think that I am doing something wrong with the operator * announcement, but I do not understand what I am doing.

Can anyone tell me how can I correct this error?

Thank you for your reply. Arithmetic operators C ++ are not automatically commutative, thus your operator only when you < Code> Atmospheric data do not have * qreal , but when type of commands are inversion (which is in 3 * ( expression) expression).

You have to type the operator * to handle the qreal * atmospheric data case, which should be written as a free function because the left The type of hand type is not of your class type.

  Inline atmospheric data operator * (const quary & lhs, const atmospheric data & amp; rhs) {// just forward to other operator (this works because I swap operands ) Return rhs * lhs; }   

By the way, IMHO should follow the normal pattern of implementing the assigning versions ( * ) in order to implement mathematical operators , And then call them with the "normal" ( * ) version; See for more details.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -