重载操作符*出错
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
时,操作符才会生效,而当类型的顺序相反时(这就是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*blah
和2*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
可以工作,y
是AtmosphericData
类型的对象。
如果您希望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
}
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 当有额外的括号时,在临时对象上调用用户定义的操作符+出错
- 用new操作符定义std::shared_ptr时出错
- 在b树的自定义迭代器中使用操作符*出错
- 使用jsoncpp库使用/操作符创建JSON字符串时出错
- 定义模板化操作符重载时出错
- 重载操作符*出错
- 重载操作符时出错