y2k
y2k

Reputation: 66016

Error c6277 overloading && in C++

In my class I have member functions:

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)

But I get

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

Upvotes: 0

Views: 136

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110738

Firstly, k1 && k2 will be evaluated to a boolean value, and then you will have that_bool && k3, which you do not provide an overload of operator&& for (and shouldn't!). It seems that what you really want to do is not overload anything at all:

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

Alternatively, you could provide a explicit conversion to bool as a member of KinematicVariable:

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

To do this in C++03, use the safe-bool idiom.

Upvotes: 5

Related Questions