2013-03-03 52 views
0

在我的课堂我有成员函数:错误c6277用C重载&& ++

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

然后我尝试

if (k1 && k2 && k3) 

,但我得到

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

这是在'KinematicVariable'类中吗?你必须有一个for(bool,const KinematicVariable&),因为第一次调用返回一个布尔值以供第二次使用。 – chris 2013-03-03 17:35:12

+0

是的,就在标题内。 – y2k 2013-03-03 17:35:28

+3

仅供参考,重载'&&'或'||'是[认为不好的做法](http://stackoverflow.com/questions/5133114/overloading-logical-operators-considered-bad-practice)。 – 2013-03-03 17:35:51

回答

5

首先,k1 && k2进行评估到一个布尔值,然后你将有that_bool && k3,你没有提供的超载(and shouldn't!)为。看来你真的想要做的是不超载任何东西:

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

或者,你可以bool提供一个显式转换为KinematicVariable成员:

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

要做到这在C++ 03中使用safe-bool idiom