Reputation: 1
I've a little problem with the overloading of an operator. I've a class named AtmospheridData, in which I define the operator *.
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:
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
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.
Upvotes: 0
Views: 104
Reputation: 234444
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*=
.
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
}
Upvotes: 0
Reputation: 361412
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 *
. That is, 3 * y
wouldn't work, but y*3
would work where y
is an object of type AtmosphericData
.
If you want 3*y
to work as well, then define a non-member function as:
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
. Otherwise, non-member non-friend should your first choice.
Upvotes: 0
Reputation: 126787
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).
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.
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.
Upvotes: 4
Reputation: 132994
You have defined an operator *
whose first argument is AtmosphericData and the second is a number. 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;
}
Upvotes: 0
Reputation: 13382
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.
Ddefine an operator which takes a left hand side of int and right-hand side of AtmosphereicData.
Upvotes: 0