错误 c6277 重载 && C++

Error c6277 overloading && in C++

本文关键字:C++ 重载 c6277 错误      更新时间:2023-10-16

在我的类中,我有成员函数:

const bool operator&&(const KinematicVariable &right) const { 
        return this->isUsed() && right.isUsed(); 
}
inline const bool isUsed() const { return this->_used; }

then I try

if (k1 && k2 && k3)

但是我得到

error: C2677: binary '&&' : no global operator found which takes type 
'KinematicVariable' (or there is no acceptable conversion)

首先,k1 && k2将被求值为布尔值,然后您将有that_bool && k3,您不会为operator&&提供过载(也不应该!)看起来你真正想做的是不要重载任何东西:

if (k1.isUsed() && k2.isUsed() && k3.isUsed())

或者,可以提供对bool作为KinematicVariable成员的显式转换:

explicit operator bool() const { return isUsed(); }