gpt4 book ai didi

c++ - C++ 中的错误 c6277 重载 &&

转载 作者:太空宇宙 更新时间:2023-11-04 16:22:43 26 4
gpt4 key购买 nike

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

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)

最佳答案

首先,k1 && k2 将被评估为 bool 值,然后您将得到 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 .

关于c++ - C++ 中的错误 c6277 重载 &&,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15188513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com