简体   繁体   English

重载运算符时出错*

[英]Error in overloading operator *

I've a little problem with the overloading of an operator. 我对运算符的重载有一点问题。 I've a class named AtmospheridData, in which I define the operator *. 我有一个名为AtmospheridData的类,在其中定义了运算符*。

In the header I define this method inside the class: 在标题中,我在类中定义此方法:

//! Operator * (scalar)
AtmosphericData operator*(const qreal& qrMult) const;

and the definition, in the .cpp file, is the following: .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;
}

Then, I use the class in the following expression: 然后,在以下表达式中使用该类:

AtmosphericData c2;
AtmosphericData t1;
AtmosphericData t2;
AtmosphericData y0;
AtmosphericData y1;
qreal           hx;

/* other code */

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

When I compile (using qmake-gcc under linux) I obtain the following error 编译时(在Linux下使用qmake-gcc),出现以下错误

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

I seems that I'm doing something wrong with the operator * declaration, but I don't understand what I am doing wrong. 我似乎在运算符*声明中做错了,但是我不明白我在做什么错。

Can anyone tell me how I can correct this error? 谁能告诉我如何纠正此错误?

Thanks for your replies. 多谢您的回覆。

Arithmetic operators in C++ are not automatically commutative, thus your operator kicks in only when you do AtmosphericData * qreal , but not when the ordering of the types is the opposite (which is what happens in the 3 * (y0 - y1) expression). C ++中的算术运算符不是自动交换的,因此,只有在执行AtmosphericData * qreal ,运算符才会AtmosphericData * qreal ,而当类型的顺序相反时(在3 * (y0 - y1)表达式中会发生这种情况),运算符不会AtmosphericData * qreal

You have to write also an operator* to handle the qreal * AtmosphericData case, which must be written as a free function because the type of the left hand operand is not of the type of your class. 您还必须编写一个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;
}

By the way, IMHO to implement the mathematical operators you should follow the usual pattern of implementing first the assigning-versions ( *= ), and then call them from the "normal" ( * ) version; 顺便说一句,恕我直言,要实现数学运算符,您应该遵循通常的实现方式:首先实现分配版本( *= ),然后从“常规”( * )版本中调用它们; see the operator overloading FAQ for more details. 有关更多详细信息,请参见运算符重载常见问题解答

If you write : 如果您写:

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

instead of 代替

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

then it should work. 那么它应该工作。 Because the definition of operator* in your code requires that the object of type AtmosphericData should be on the left side of * . 因为在代码中定义operator*要求是AtmosphericData类型的对象应位于*的左侧。 That is, 3 * y wouldn't work, but y*3 would work where y is an object of type AtmosphericData . 也就是说, 3 * y不起作用,但是y*3将起作用,其中yAtmosphericData类型的对象。

If you want 3*y to work as well, then define a non-member function as: 如果希望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!
}

I would suggest you to define other function as non-member function as well. 我建议您也将其他函数定义为非成员函数。 And if needs to access private or protected members, then make it friend . 如果需要访问私有或受保护的成员,则使其成为friend Otherwise, non-member non-friend should your first choice. 否则,非会员非朋友应该是您的首选。

You are trying to multiply a AtmostphereicData by an int ( 3*blah and 2*blah ), an operation which you don't appear to have defined. 您正在尝试将AtmostphereicData乘以一个int( 3*blah2*blah ),您似乎尚未定义该操作。

Ddefine an operator which takes a left hand side of int and right-hand side of AtmosphereicData. D定义一个运算符,该运算符使用int的左侧和AtmosphereicData的右侧。

You have defined an operator * whose first argument is AtmosphericData and the second is a number. 您已经定义了一个operator *其第一个参数是AtmosphericData,第二个参数是数字。 But NOT vice versa. 但反之亦然。

You should define the other operator as well (as a nonmember) 您还应该定义其他运算符(作为非成员)

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

If you declare the operator inside the class, it will only work if you use your class on the left . 如果在类中声明运算符,则只有在使用左侧的类时,该运算符才有效。 To use your class on the right you need to also overload the operator as a free function: 要在右侧使用您的类,您还需要将运算符重载为自由函数:

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

You could also use Boost.Operators and get all this for free with operator*= . 您还可以使用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